I'm trying to replace a few empty href="" statements with computed URL's on pageload.
The replacement string, however, contains several &'s, which need to be in the href.
What ever I try, as soon as setAttribute("href", url); or $(id).attr('href', url) is used, all &'s are replaced by a & and the links fail.
How can I prevent this from happening? I'm pretty sure any code that ends in .href or contains href is going through a translation stage and will all fail.
You might have noticed that I've edited this message a lot of times because you simply cannot type a full '& a m p ;' in this box without it being translated to a '&' here too.
Let's step back a bit, and think about this from the ground up. The escape sequence &xxx; is the way to embed "special characters" into an HTML document. To be clear, it is an HTML escape sequence.
This way of escaping characters is useful because it means characters can be placed into an ASCII file that would otherwise be impossible to embed.
For example, the Euro sign (€) can be embedded using a numeric code representing its Unicode codepoint €. Or, a non-breaking space can be embedded using its character entity name, . (Aside: these characters would be easier to embed in a UTF-8 file, with no escaping needed, but that's beside the point here)
Now, because this escape sequence always begins with an ampersand, it means that a bare ampersand in an HTML document is ambiguous - as the browser reads through the document it doesn't know if you want an actual ampersand or whether there's an HTML entity escape sequence coming up. To remove this ambiguity, if you want to embed an ampersand in an HTML document then you need to escape it.
Let me highlight that sentence: To remove this ambiguity, if you want to embed an ampersand in an HTML document then you need to escape it.
Because this escaping only applies to HTML documents, if you are writing Javascript in a .js file then this escape sequence is irrelevant to you. You can just drop your ampersands in unescaped and there's no problem. What it means is, your Javascript files can replace the URLs with bare ampersands, no & needed.
As far as the browser is concerned, all of those URLs you write only contain bare ampersands. The escape sequence & is only needed to express those in HTML form, as soon as the browser has read the HTML then that escape sequence is forgotten about.
I've repeated myself a few times here but that's because I've tried to make it as clear as possible.
You have to replace & with & in the url, prior to setting href property of an a tag.
var url = 'www.example.com?a=1&b=2'
url = url.replace(/\&/g, '&')
$('a').attr('href', url)
Related
I'm trying to figure out what is the proper way to use the "not" operator (Negation) which looks like: ¬ (UTF-8: U+00AC) in JavaScript. I could use:
<span>¬A</span>
But I'm not sure if it is the proper way and if its been supported on all (or most) of the modern browsers and mobile. I tried to find a previous topic on this matter but could not find any. The wanted result is to display this operator beside A.
Correct escape sequences for HTML are: ¬, followed by ';A', and for javascript - either '\xACA' or '\u00ACA'.
"meta charset" is always advisable to include, but it does not affect the escapes.
I added ';' after the HTML escape sequence: for some reason it wasn't needed if instead of 'A' there is, for example, 'k', I didn't know. Semicolon has to be added.
If you are properly using UTF-8 (which is to say that your HTML, JS, and CSS files are all encoded in UTF-8, <meta charset="utf-8"> is present in the <head>, and HTTP headers all define the charset as UTF-8), there should be no need to encode the symbol at all. Simply write ¬, exactly as you have done here.
The only symbols you need to encode are < and & (also ' and/or " in attributes). Most would also recommend encoding > and the non-breaking space, to avoid confusion. I encode all of these, but nothing else.
I have an HTMl string that is stored in a database. When I go to set the value of a javascript variable with this string on the front-end via my templating engine (Leaf), it stores escaped as:
var string = <p>It's a round about way.</p> <p><!-- pagebreak -->But Maybe this is the way?</p>;
I'm trying to set this value as the content value for TinyMCE, but JavaScript produces an Unexpected EOF error when reading this string and points to a & character, which I presume is the first character of the new line. I tried on the back-end to replace occurrences of string \r\n with a so it would play nicer with JavaScript but the changes didn't seem to take. I tried encoding/decoding the string but that didn't help. Perhaps someone can help shed some light on this seemingly trivial task?
Thanks in advance.
Javascript was rendering \r\n characters found in the string instead of escaping them. Parsed it out in the server-side code instead of handling in JS.
I don't have to HTML encode the string. I was trying various solutions, but the problem is still, how do you handle the semicolon entered by the user if you need to do a JS str.indexOf(";"); later on.
I’m using System.Net.WebUtility.HtmlEncode(test); encode my string,which adds semicolons (as you would expect for html encoding).
Later down the process this string gets utilized to create JavaScript commands that end with ';'. These commands are separated by doing a str.indexOf(";");
My issue is that the user is allowed to enter semi-colon in the field,which breaks the aforementioned indexof(";"), which I use to dynamically create the JavaScript commands.
How can I support users entering in semicolons into a string if I need to do a JS indexof(";") to separate the JS commands?
I tried in the C# side doing a
string myString = System.Net.WebUtility.HtmlEncode(test);
but that just makes the situation worse by adding even more semicolons as you would expect for HTML enconding.
The solution I came up with was to do a replace on the the C# side. In C# I do a .Replace of all % and (other problematic characters) with their URL encoded string versions before the JavaScript command ending ";" gets inserted(i.e. myString.Replace(";","%3B").Replace("=","%3D");).
Once it hits the JavaScript side I do the complete opposite, thus leaving my JS semicolons intact.
The aforementioned solution allowed me to distinguish between a user inserted semicolon and one entered in programmatically.
I'm having some difficulty narrowing down the problem, I am wondering if anyone can help.
We are storing a URI in a element href attribute. This URI is dynamically created. One of the URI path variables maps to an id of an element in our database. Recently, we have started to use ampersands on special occasions in the id. The URI looks something like "/{entityType}/{entityId}/moderation".
When the URL is built, we use the encodeURIComponent javascript function, effectively turning the ampersand into '%26'. By examining the data stored in the href attribute via a web development tool, it looks like it is stored correctly. However, when I mouse hover it only displays the ampersand in the url.
Other ids work fine, including things like single apostrophes, but with an ampersand, it looks like it is getting a page redirection error (I'm guessing the ampersand is making the url invalid). I've tried escaping the percent sign, making the ampersand turn into '%2526', thinking that the %25 portion will be decoded to just the percent sign (making the final result %26).
So far none of my tinkering has worked. Anyone have a suggestion for next steps or what may be going on? Any help appreciated!
I would like to change string encoding from UTF-8 to ISO-8859-2 in Javascript. How can I do it?
I need it because I've designed a widget. User just copies < script > tag from my site and puts it on his. This script creates div and puts into div widget contents with text.
If target website is in UTF-8 encoding - it works fine. But when it is in ISO-8859-2 than text that is encoded in UTF-8 is displayed on site with ISO-8859-2 and as a result I see trash.
Instead of using e.g. "ĉ" in your JavaScript code, use Unicode escapes such as "\u0109".
If you're in control of the output, you can replace all special characters with unicode escapes (e.g. \u00e4 for ä). The browser can interpret it correctly regardless of document encoding.
The easiest way to do this would be to put the string into a JSON encoder. Both PHP's and Ruby's does that. Don't know about other implementations though.
Another solution that might work is to add charset="utf-8" to the <script> tag.
I suppose you just need to convert your wdiget from UTF-8 to ISO-8859-2 and provide 2 versions of script.