" raises an error in IE If I add this html snippet to my page: '

The line "</script>" raises an error in IE

If I add this html snippet to my page:

<script type="text/javascript">
 var s = '</script>'
</script>

      

IE 7.0 shows syntax error (exclamation mark at bottom left): "Unterminated string constant"

If I only change one letter (any), the error goes away - it looks like IE doesn't like that particular word, including the brackets.

Any ideas why?

Thank you, Andrey

+2


source to share


5 answers


This works for me



var a = "<\/script>"

      

+8


source


This will happen for any browser. The HTML parser does not know the details of the scripting language you are trying to use, so the tag <script>

will be aborted the first time it appears </script>

, regardless of context. The JS parser will then of course complain that the line is not completed because the closing apostrophe is not inside the script block.



You need to use something like instead '<\/script>'

if you want to use that line in your script.

+6


source


To use the / character, you first need to type it with \

.

So this works:

<script type="text/javascript"> var s = '<\/script>'; alert( s);</script>

      

+3


source


I saw this...

var s = '</scr' + 'ipt>'

      

It does give a bit of code smell, though. I'm not sure if this is appropriate. :)

+1


source


I saw this:

var s = unescape("%3C/script%3E")

      

The smells are very bad.

0


source







All Articles