admin管理员组

文章数量:1289986

Here's the entire document that causes the error in Firefox and Chrome:

<!DOCTYPE html>
<html>
<head>
    <script>
        var strs = [], scripts = ['harbl.js'], s = 0;
        strs.push('<script src="' + scripts[s] + '"></script>');
    </script>
</head>
<body>
    <p>buh...</p>
</body>
</html>

/

The error I get is:

Uncaught SyntaxError: Unexpected token ILLEGAL 

This has me really puzzled.

Here's the entire document that causes the error in Firefox and Chrome:

<!DOCTYPE html>
<html>
<head>
    <script>
        var strs = [], scripts = ['harbl.js'], s = 0;
        strs.push('<script src="' + scripts[s] + '"></script>');
    </script>
</head>
<body>
    <p>buh...</p>
</body>
</html>

http://jsfiddle/cryptoquick/J4zZT/

The error I get is:

Uncaught SyntaxError: Unexpected token ILLEGAL 

This has me really puzzled.

Share Improve this question asked Oct 1, 2012 at 18:38 Hunter BeastHunter Beast 7817 silver badges18 bronze badges 1
  • @Sednus what is the point of that? – Ian Commented Oct 2, 2012 at 3:25
Add a ment  | 

3 Answers 3

Reset to default 9

The </script> literal is parsed by the browser as it's not aware of the context when traversing your code. You have to escape it like so : <\/script>. It's a known problem.

Change it to:

strs.push('<script src="' + scripts[s] + '"></s' + 'cript>');

Front Slash '/' is considered as special character . It's the cause for the error .

strs.push('<script src="' + scripts[s] + '"><\/script>');

Please read this post for more information JavaScript and forward slashes in strings .

本文标签: