admin管理员组文章数量:1428258
We have some custom JS scripts to deal with tooltip which are put in a dom attribute (tooltip) when we render the page.
however when i try to retrieve this tooltips (displayed as divs) string javaScript seems to automatically unescape the attribute value. is this normal behavior? and is there a way to avoid this?
The problem i have is that <[email protected]> turn into (invalid) html.
example for reproduction:
<div tltip ="<text>" id="escaped" />
<div tltip ="<text>"id="notescaped" />
js:
a = document.getElementById("escaped").getAttribute("tooltip");
b = document.getElementById("notescaped").getAttribute("tooltip");
a.match("<"); //returns true (unexpected)
a.match("<"); //returns true (expected)
a == b; // returns true (unexpected)
edit:
to clarify im trying to display a (div) tooltip where i would like to somehow read content such as:
"<b> <[email protected]> <\b>"
from the dom and display it in a div where it should look like: "<[email protected]>"
We have some custom JS scripts to deal with tooltip which are put in a dom attribute (tooltip) when we render the page.
however when i try to retrieve this tooltips (displayed as divs) string javaScript seems to automatically unescape the attribute value. is this normal behavior? and is there a way to avoid this?
The problem i have is that <[email protected]> turn into (invalid) html.
example for reproduction:
<div tltip ="<text>" id="escaped" />
<div tltip ="<text>"id="notescaped" />
js:
a = document.getElementById("escaped").getAttribute("tooltip");
b = document.getElementById("notescaped").getAttribute("tooltip");
a.match("<"); //returns true (unexpected)
a.match("<"); //returns true (expected)
a == b; // returns true (unexpected)
edit:
to clarify im trying to display a (div) tooltip where i would like to somehow read content such as:
"<b> <[email protected]> <\b>"
from the dom and display it in a div where it should look like: "<[email protected]>"
- 4 I think the browser is doing this automatically when it parses the HTML and creates the DOM. – Felix Kling Commented Jun 27, 2012 at 10:43
-
You shouldn't really need the encoded characters. Just add the attribute value to the
<div>
as a text node, not usinginnerHTML
. – millimoose Commented Jun 27, 2012 at 12:36 - the problem is i dont want it as a text node i want the bold tags to work als bold, but i dont want to have a email address in brackets interpreted as (invalid html), this is why the email bracket are htlmEscaped in the attribute (and the bold tags not). my problem lies in that both the escaped and the unescaped brackets are treated the same. – pvgoddijn Commented Jun 27, 2012 at 13:36
- i'm currently double escaping the emailaddress but this feels kinda hacky – pvgoddijn Commented Jun 27, 2012 at 13:37
1 Answer
Reset to default 6It's not JavaScript which "unescapes" the characters, but the HTML parser which decodes the entities. It's expected behaviour.
If it does not sound logical to you, tell me how you would put a double and a single quote in an attribute.
<div tltip=""'"> does not work.
<div tltip=""'"> does work.
<div tltip='"''> does work.
<div tltip=""'"> does work.
Regarding the edited question
You don't have to read the original source code. For instance:
<div tltip="<b>test</b>" id="test"></div>
var output1 = document.getElementById('html');
var output2 = document.getElementById('plain');
var attrValue = document.getElementById('test').getAttribute('tltip');
output1.innerHTML = attrValue;
output2.textContent = attrValue;
Will render as:
Output 1: test
Output 2: <b>test</b>
textContent
is not supported in old IE versions. A cross-browser patible way is:
output2[document.textContent === null ? 'textContent' : 'innerText'] = attrValue;
// OR
output2.innerHTML = ''; // Empty contents
output2.appendChild(document.createTextNode(attrValue));
If you still want to decode the entities, then the following will work:
// <b> -> <b>
function decode(strHTML) {
var tmp = document.createElement('div');
tmp.appendChild(document.createTextNode(strHTML));
return tmp.innerHTML;
}
本文标签: javascriptgetAttribute(quotnamequot) unescapes htmlStack Overflow
版权声明:本文标题:javascript - getAttribute("name") unescapes html? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745522957a2661710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论