开发者

IE javascript html encoding weirdness

开发者 https://www.devze.com 2022-12-14 23:58 出处:网络
Put this in your location bar in IE javascript:location.href=\'http://www.google.com/search?q=something&lt=bar\'

Put this in your location bar in IE

javascript:location.href='http://www.google.com/search?q=something&lt=bar'

Watch in horror as it helpfully thinks &lt actually means &lt; and makes it a <.

WTF is going on here?

Other browsers don't do开发者_C百科 this. How do I prevent IE from doing this?


Since this is part of an HTML document you should escape the & by replacing it by &amp;

An ampersand '&' in HTML mark the start of a character reference entity such as &lt; &gt; and &eacute;

Literal ampersands in your document should be written as &amp;

Note that if that code is generated dynamical you should also escape the URL so that it can appear in a JavaScript string as well.

Edited: I can confirm this particular problem occurs when pasting JavaScript code directly in the address bar. It seems HTML character escaping rules are irrelevant here after all.

I've googled a bit and found this blog post which identify that particular behavior as an ie bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消