开发者

Escaping HTML in jQuery so it's valid

开发者 https://www.devze.com 2023-02-16 13:25 出处:网络
I\'ve followed advice from firebug and W3C links, however when Firebug confirms all is valid, W3C does not seem to like it and vice versa.

I've followed advice from firebug and W3C links, however when Firebug confirms all is valid, W3C does not seem to like it and vice versa.

All I'm doing is this:

return '<a href="#"></a>';

I believe I should use a backslash infront of the forward slash, though as XHTML, the start tag needs to be escaped as well? 开发者_开发问答Can anyone shed any light on this.

Thanks.


Short answer: Don't use XHTML on the client.

Almost the short answer: Put your JavaScript in external files

Longer answer: If you really want to embed the JS, then wrap it with CDATA flags

Really long answer: Read http://dorward.me.uk/www/comments-cdata/


If you wrap JS in CDATA tags, you don't need to escape the HTML.

<script type="text/javascript">
/* <![CDATA[ */
function(){
    return '<a href="#"></a>';
};
/* ]]> */
</script>
0

精彩评论

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

关注公众号