0

I want to print or echo back code to the user, but when I attempt to I get an ILLEGAL error. How would I go about showing the below code as just normal text instead of executing it:

alert('<script>hello</script>');

I've been trying to print it to a text box with jQuery when I get the error, like so:

$('.printed-code').val('<script> write your code between these lines </script>');

3
  • jsfiddle.net/WgxrW/1 Commented Jul 27, 2013 at 14:01
  • Just out of curiosity: why do you need this? It seems an akward thing to want to do. If it is for debugging, there are better ways :) I do not understand why it is really necessary to show the script tags (they are causing the problem). If it is a code block you want to show, use pre tags, and show code. Commented Jul 27, 2013 at 14:05
  • I'm writing a WordPress plugin where users can select select options and input things and then generate a set of code that they can then use in themes and templates Commented Jul 27, 2013 at 14:17

4 Answers 4

3

If you are placing your JavaScript in a <script> element, then the </script> in the string literal will be recognised as a matching end tag for the script. Break up the string with an escape sequence (in a JS string literal, \/ means the same as /):

$('.printed-code').val('<script> write your code between these lines <\/script>');

… or keep the script in an external file and reference it with <script src="foo.js"></script>.


If you want to display information to a user, then you shouldn't be using an <input> to do so, so you will be wanting the text() method, not the val() method. (Assuming .printed-code matches a <code> element or something else that is appropriate for the task at hand).

$('code.printed-code').text('<script> write your code between these lines </script>');
Sign up to request clarification or add additional context in comments.

1 Comment

great answer, exactly what I was looking for! $('.printed-code').val('<\script> write your code between these lines <\/script>');
1

You should escape the / like this: <\/script>. Otherwise you get errors, especially in old browsers where </ ends the script block (not </script>)

Comments

0

Someone answered earlier with an exact code example, and included a jsfiddle. He deleted the post for some reason but this was the code he provided. I would have marked that as correct. thank you all for the input

$('.printed-code').val('<scr'+'ipt> write your code between these lines </scr'+'ipt>');

2 Comments

Don't do that. Breaking up the start tag is unnecessary and using string concatenation to break up the end tag do this is messy and inefficient.
Why include the <script> tags in the first place?
-2

The script tags are being interpreted literally.

Try:

alert('&lt;script&gt;hello&lt;/script&gt;')

1 Comment

alert doesn't convert HTML entities to text, they are shwon as they are. Not my downvote though.

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.