Html entities must be encoded in alt attribute of an image in HTML page. So
<img id="formula" alt="A → B" src="formula.png" />
will work well.
On the other hand, the same JavaScript code will not work
document.getElementById('formula').alt = 'A → B';
and will produce A → B instead of A → B.
How to do it through JavaScript, when it is not possible to put the special (unencoded) characters in the source code?
-
Google peoples - see also: stackoverflow.com/questions/94037/…Andrew– Andrew2017年06月21日 20:57:08 +00:00Commented Jun 21, 2017 at 20:57
4 Answers 4
JavaScript has its own system for escaping special characters in strings:
document.getElementById('formula').alt = 'A \u2192 B';
4 Comments
→ 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.innerHTML to &#rarr; or → because all HTML parsers replace entity references with their text equivalent: you never get a DOM EntityReference node.Here's an alternative if you can't save your file using a Unicode format:
function decodeHTML(str) {
return str.replace(/&#(\d+);?/g, function() {
return String.fromCharCode(arguments[1])
});
}
However, this requires you to use the numeric representation. In this case →.
6 Comments
↑ to ↑ on server side).→ escapes in HTML. If you really want to use the decimal codes you can: 'A '+String.fromCharCode(8594)+' B'.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Decode HTML entities using JavaScript</title>
</head>
<body>
<img id="formula" src="vote-arrow-up-on.png" alt="A → C">
<script>
function html_entity_decode(str) {
var p = document.createElement("p");
p.innerHTML = str.replace(/</g,"<").replace(/>/g,">");
return p.innerHTML;
}
var theValue = html_entity_decode('A → B');
document.getElementById('formula').title = theValue;
</script>
</body>
</html>
Credits to The JavaScript Source.
EDIT: I changed the original code to only use innerHTML on a <p> element, instead of innerHTML and value of a <textarea>.
6 Comments
title attribute ;)textarea should not change its form field value according to the standard DOM; the contained HTML maps to the textarea's defaultValue property, not value. It's a browser quirk that innerHTML also affects value, and it doesn't happen in Opera. Don't rely on it. You could write innerHTML to any other element and read the text node child it ends up with (if non-blank string). Also, avoid setAttribute, which is unnecessary in HTML documents, and buggy in IE.el.getAttribute(someattr)/el.setAttribute(someattr, ...)) as being the same as property access, el[someattr]. Consequently, (1) any property whose value isn't a string will expose something that isn't a string. For integer properties you sometimes don't notice as weak typing hides it from you, but booleans, event handler functions and style will often trip you up. (2) any property whose name is different from the corresponding attribute will fail. That's htmlFor, className, and any attribute name made from multiple or hyphenated words.href will return the full resolved URL in a link, not the original, possibly-relative version that was actually used in the attribute value, and value returns a form field's current value, and not the content of the value attribute (which actually maps to defaultValue). Also (4) custom-attributes can shadow real properties, so writing <div nodeType="3"> might confuse a script into thinking it's a text node.getAttribute exclusively to access them, and if you've got any sense you'll limit this use to attributes prefixed data-.)In that particular case, you don't have encode special HTML characters in JavaScript.
The W3C validator should not complain about this (just tested it) and the document should validate. If not post your code and I'll update my answer.
1 Comment
Explore related questions
See similar questions with these tags.