开发者

emit ampersand to html page in javascript

I want to emit an '&' without the '&a开发者_JAVA技巧mp;amp;' on the page

document.write('&');

emits &

document.write('\&');

emits &

Proof in the generated html

<html xmlns="http://www.w3.org/1999/xhtml"><head>


    <title></title>
</head><body>
<script type="text/javascript">

    document.write('&');
    document.write('\&');

</script><div firebugversion="1.5.4" style="display: none;" id="_firebugConsole"></div>&amp;&amp;
</body></html>


wcpro: "im trying to emit a url on the page ?this=1&that=2, you are telling me that this is not possible?"

Well, technically the ampersand should be encoded as &amp; in your html-code:

<a href="somewhere?this=1&amp;that=2>something</a>

When your browser reads the page it decodes &amp; to & and that's what you get when you click on the link.

But what about a single ampersand then?

<a href="somewhere?this=1&that=2>something</a>

It's wrong, and not legal html. It works because this mistake was so very common that the browsers correct it for you. Just like they try to correct other things like missing tags and all other bad things with the tag-soup.

W3c suggests the use of semicolon instead of ampersand to get rid of this problem.

If you ever try XHTML you will be notified of every single error.


Can't be done. Drop this on your page:

<span id="and">&</span>

and call alert(document.getElementById("and").innerHTML)

You'll see &amp; Your browser is automatically converting & to &amp; when it isn't a valid escape sequence.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜