Timeline for answer to Alt attribute encoding with JavaScript by s4y
Current License: CC BY-SA 2.5
Post Revisions
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 6, 2010 at 15:57 | comment | added | s4y | @bobince: I think it would be great if authors could comfortably include any character in a document, but I've seen way too many encoding fails to recommend it, and I'd only do it myself if I had control over the website, server, VCS, and editors used by the developers. "Special characters" means every character that's at risk for being broken by bad encoding, everything but the ASCII 95. | |
| May 6, 2010 at 14:06 | comment | added | bobince |
Yeah, my feeling is there's a common assumption that non-ASCII characters need escaping which is much less often true than people think. (Indeed, the phrase 'special characters' is itself a misnomer when we're talking about 99.9% of all characters...) In any case there will be no difference between setting innerHTML to &#rarr; or → because all HTML parsers replace entity references with their text equivalent: you never get a DOM EntityReference node.
|
|
| May 5, 2010 at 22:45 | comment | added | Marcel Korpel | @bobince: That was my first answer, too, but the question explicitly states: "How to do it through JavaScript, when it is not possible to put the special (unencoded) characters in the source code?" Moreover, this solution presupposes the source is a Unicode value, but the question's example uses an HTML entity. Oh, and really, no offence meant, I'm just wondering what to do in such a situation. | |
| May 5, 2010 at 19:16 | comment | added | bobince |
+1. When you are assigning DOM properties, you don't need HTML-encoding. If you want the character → just paste it in; it's perfectly valid to say img.alt= 'A → B';. That only requires you to get the encoding you're saving your page as to match what you're serving it as (best: use UTF-8 for both). The JavaScript string literal escape 'A \u2192 B' is a good fallback if you can't rely on non-ASCII characters being served properly.
|
|
| May 5, 2010 at 18:02 | history | answered | s4y | CC BY-SA 2.5 |