1

I am writing HTML and JS for a small site. When I write some special words in JS, I encounter a problem.

Here is the sample:

<script Language="javascript" >
    var template = {
                    "role":'<role name="" />',
                    "script":'<script>xxx</script>'
                   };
    ...
</script>

I found that Chrome will parse it wrong. It will ignore the JS text after the file </script> in the template declaration.

1
  • 1
    Tried this: '<' + '/script>' ? Commented Sep 18, 2013 at 6:10

2 Answers 2

4

The browser sees the </script> in your script as the end of the script tag, not as a string. That means you are essentially trying to execute this:

<script>
  var template = {
      "role":'<role name="" />',
      "script":'<script>xxx
</script>

which is invalid JavaScript.

To avoid this, you need to split up your script tag:

"script":'<script>xxx</sc' + 'ript>'

so that the browser does not see the </script> and properly waits until your intended closing script tag.

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

Comments

1

It should be like this :

<script type="text/javascript" >

        var template = {
                           "role":'<role name="" />',
                           "script":"<sc"+"ript> type='text/javascript'>xxx"+"</sc"+"ript>"
                       };
    ......
    ......

</script>

1 Comment

I don't see any change except type attribute which is anyways not required if OP is using HTML5 doctype

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.