Bookmarklet Window.Location change not behaving - javascript

Sorry folks, I've been poking around StackOverflow for awhile but can't seem to garner enough information to fix my problem here. I'm not very adept with regex, or the use of javascript in bookmarklets. Hoping someone can help me fix this little problem.
I would like this:
http://www.example.com/a/b/c/d/review?set=123456
To become this:
" " " " " /close?set=123456
Obviously placeholder information (not that hard to figure out what it's for haha), but the sections abcd are ALWAYS there and always the same, it's just the very end piece that changes while retaining the variable number at the end.
Here's what I have that doesn't work:
javascript:window.location=window.location.href.replace(/.+set=(\d+).+/i,'http://www.example.com/a/b/c/d/close?set=$1');
Every time I use the bookmarklet, instead of loading the page properly, it just falls flat and does nothing. However if I manually change the correct piece of the URL, it behaves properly... I think I'm doing something wrong and I think it's very simple.
The numbers at the end are variable in both value and number. Every other piece remains static.
Any advice?

Quicker but no regex:
javascript:window.location.href = window.location.href.replace("review?","close?");

You probably want to replace
/.+set=(\d+).+/i
with
/.+set=(\d+).*/i
The + matches one or more of whatever precedes it.

Related

Randomized Divs each time a page is reloaded/refreshed

I saw this post:
Random Div Order on Page Load
and the working solution was posted:
jsfiddle.net/BwJHj/1/
Which works each time I refresh that page ^ As soon as I make my own, or even just copy the exact HTML/CSS/JAVA from that demo, it doesn't work?
Just wondering why that may be.
Select Javascript from settings and it will work
without javascript : [https://jsfiddle.net/UserIsMonica/daw5nytd/][1] (no randomize)
with Javascript : [https://jsfiddle.net/UserIsMonica/daw5nytd/1/][2] (Randomize)
It's common first time Jsfiddle user can easily miss that. i did :)
happy coding !!

JavaScript: how to set a value containing quotes inside a textbox

I have an MVC-project where I store data in a database and one of my views contains textboxes to edit this data.
Because of the specifics I can't create the textboxes directly via TextBoxFor(), EditorFor() etc. but have to affect the value with JavaScript, so what i do is write the needed value in the javascript code at page loading, and this code is later on triggered to affect the textbox value.
$("#textboxID").val("#HTML.Raw(Model.value)");
This workes fine until one of strings has got quotes in it.
When i input it directly like
$("#textboxID").val("#Model.value");
it will be HTML-encoded with the quotes written as > & quot; (without space of cource)
What i found out is that the only way to output quotes correctly is by escaping them with backslash \ however i can't seem to find a helper to do that.
Is there a solution? Am i doing anything wrong?
For now, i found a workaround inspired by Filipe Borges suggestion
#Html.Raw(Html.Encode(Model.Libelle).Replace(""", "\\\""))
It's very ugly, but at least it solves the problem, I appreciate anyone suggesting a better solution.
For now, i found a workaround inspired by Filipe Borges suggestion
#Html.Raw(Html.Encode(Model.Libelle).Replace(""", "\\\""))
It's very ugly, but at least it solves the problem, I appreciate anyone suggesting a better solution.
Solved using:
#Html.Raw(Html.Encode(Model.value).Replace(""", "\\\""))
First suggested replacing " by \" using #Model.value.replace(""", "\\\""), but it does not work because the value only contains " after the default html encoding is applied by mvc.
Edit: Final solution by Souhaieb Besbes. Edited mine to not keep it wrong.
You can use single quote to show double quotes as following:
$("#textboxID").val('"'+#Model.value+'"');

Adding javascript attributes to an anchor

i have search before but didnt find the exact case
i would like to add a javascript attribut to a href
exemple:
my site
i need to add this attribut:
onclick="this.href='http://somesite.com'"
result
my site
could you help me to process this please ?
thanks you very much
Before I begin, let me mention that inline javascript in not recommended and is frowned upon.
First, let me point out that you missed a " in your code:
my site
------------------------^
"
Now, you should have this:
my site
That should hopefully work.
Good luck.

Html/Javascript - How to make an html link display a url, and have it actually be redirected to a javascript function?

I have an html page, and I need a link to show that the user would be going to 'example.html', when really, the link goes to 'javascipt:ajaxLoad(example.html);'.
I tried this:
Example
But it didn't work. Any help? I already asked the webmasters stackexchange, and they told me that this would be a javascript programming question. Not an html question.
Example
By returning false you prevent the default action. And this way the links will still work when javascript is disabled, but then you don't get the AJAX functionality.
Just point the href at the actual file. The javascript onclick will take precedence - as long as you take care to disable the actual click effect by doing a "return false" or similar, the status bar will show 'example.html' and not the javascript url.
As well, note that it should be javascript:... (you're missing an r). The onwhatever attributes are already assumed to be javascript, so you could just say onclick="ajaxLoad(...) anyways.
Look, I'm not sure if I got exactly what you're asking about here, but the following fix often works with me. Just change the double-quotes to single-quotes, and put double-quotes around the example.html part
<a href="example" onclick='javascipt:ajaxLoad("example.html");'>Example</a>

Search and replace

I'm working on a new project and i want to do something similar (somehow) to intellitext ads (without popup though). In fact, i want to add links on certain words. The problem is that i don't know how to handle this.
I mean, ok, i get document.innerHTML then what? I can find any word, but some may be inside of tags. I don't want to replace words from textarea or text inside of a link. Same for input, select, and so on.
Also, the way i've managed things, i replace even the html tags (if the word is select for example, the script will replace <select> tag. And i really don't want this :P
So, any idea? Where to start ? What to do? Anything?
I wish a solution that works without any library, or, if is mandatory, to work with jquery (this is what i know).
I think that some regexp may help. The problem is... I never understand them :s
Any help will be appreciated. Thanks a lot!
jQuery does this already!
Just use .text() to get all the text underneath. Documentation and examples are here.

Categories

Resources