-1

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?

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Smet, i can't help you further until you post some code that shows what you're trying to do in JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.