1

I'm trying to display some javascript within pre/code tags using jQuery text() method. Like this:

 jQuery(".output").wrapInner( "<pre><code></code></pre>" );
 jQuery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apiKeyValue.val() + "></script>");

However the script tags produce a

Uncaught SyntaxError: Unexpected token ILLEGAL

error in the console. How can I display the script tags?

0

1 Answer 1

2

Replace

jQuery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apiKeyValue.val() + "></script>");

with:

jQuery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apiKeyValue.val() + "></"+"script>");

or:

jQuery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apiKeyValue.val() + "><\/script>");

You need to break the </script> tag ("</"+"script>") or escape the / ("<\/script>")

As pointed out by TreeTree in the comment, for a detailed explanation, please read Why split the <script> tag when writing it with document.write()?

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

2 Comments

I think you also need to replace text(".....") with text('.....') as html attributes is better to be quoted with " " like type="text/javascript"
I think an explanation of why you have to do this would go a long way. I found this stackoverflow.com/questions/236073/….

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.