rhughes
rhughes

Reputation: 9583

Javascript Error in Chrome: Me, something I'm missing or a bug in Chrome?

I doubt the last option, but probably one of the first two. Can anybody tell me which?

I'm getting the error in the screen shot.

<html>
<head>
<script type="text/javascript">
var html = "<script></script>";
</script>
</head>
<body>
</body>
</html>

Screenshot of Error

Upvotes: 1

Views: 143

Answers (3)

gabitzish
gabitzish

Reputation: 9691

You can also use \ to render that character correctly:

var html = "<script><\/script>";

Upvotes: 2

Randy the Dev
Randy the Dev

Reputation: 26740

That's no bug. It's the correct behaviour for a script tag, what you're doing is the equivalent of not escaping a quote in a string.

var string = 'My mother's awesome.';

An easy way to fix your issue is to break apart the </script> tag, like so:

var html = "<script></"+"script>";

Upvotes: 1

steveukx
steveukx

Reputation: 4368

This is a problem in every browser, the script block is terminated at the first string </script>, so if that string appears in your code anywhere it will cause a premature termination of the script block.

If you want to have this as a variable in JS, use:

var html = unescape("%3Cscript%3E%3C/script%3E");

Upvotes: 5

Related Questions