Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent jQuery .html() from escaping the href attribute automatically?

UPDATE: Seems like a browser specific behavior as TimWolla commented - how should I normalize to the unescaped version, reliably in a cross-browser manner?

HTML:

<div id="test"><a href="#{one}">#{two}</a></div>

JS:

$('#test').html()
=> <a href="#%7Bone%7D">#{two}</a>

Notice the #{one} in the href is escaped, while #{two} is not.

Is there a better way than just unescape the entire string?

unescape($('#test').html())
=> <a href="#{one}">#{two}</a>

Here's the example: http://jsfiddle.net/kenn/n8veL/

like image 293
kenn Avatar asked Nov 14 '22 10:11

kenn


1 Answers

First of all, I doubt if there is a reliable solution to your question. The underlying reason is simple: because the Element.innerHTML working underneath is non-standard and it depends solely on the browser's implementation.

If you want a reliable solution, I'd suggest you use DOM operations instead of template.

like image 115
shinkou Avatar answered May 25 '23 22:05

shinkou