开发者

Using <script> inside a jQuery-template

I'm using jQuery-templates for the first time, and I need my template to include some javascript, so it it run when the template is rendered. I need开发者_开发百科 at timestamp for the current time...

Writing

<script type="text/javascript"></script>

in a template, renders it to fail.

Is this simply not possible?


the following was blatantly copied from my answer to another question

When the HTML parser finds <script> it will start parsing the contents until it finds </script> which is present in:

document.write("<script src='links7.js?'+Math.random()+></script>

As such, you'll need to change the source so that it's not parsed as the end of a script element:

document.write("<script src='links7.js?'+Math.random()+></scri" + "pt>");

Ideally, you'd have HTML escaped all your inline JavaScript code, which would also mitigate this issue:

document.write(&quot;&lt;script src='links7.js?'+Math.random()+&gt;&lt;/script&

For your particular case (which is different enough for me to not mark as a dupe), make sure that your content between your script tags is HTML escaped, or correctly placed between <![CDATA[ ]]> tags.

&lt;script type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

-or-

/* <![CDATA[ */
<script type="text/javascript"></script>
/* ]]> */

That all being said, you should be calling the necessary JS during the rendering process, and not injecting a script element into the DOM unnecessarily.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜