0

I have no idea how to explain this, I'll just show you want I am trying to do

I have a JavaScript function that adds new content to a div

function stopUpload(success) {
  document.getElementById("load-content").innerHTML = success + document.getElementById("load-content").innerHTML;
}

however, success has a JavaScript function inside it:

success = '
        //more html
        <script type=\"text/javascript\">                   
            $(\"body\").share({
                onClickId: \"share-default-29\",
                owner: \"brfieger\",
                description: \"asdasdasdasd\",
                image: \"\",
                pid: \"29\",
                type: \"status\"
            });                     
        </script>   
        //more html
    ';

I am getting an error because it is recognizing

</script>

and is terminating the script

Uncaught SyntaxError: Unexpected token ILLEGAL

If it matters, I am calling stopUpload like:

<script type="text/javascript">
    window.top.window.stopUpload('//success content above');
</script> 

I have no idea how to fix this, I would assume the script is encapsulated inside a string and wouldn't be recognized until being evaluated, but it is

Also, for readability I made new lines. In the code all new lines are converted to \n

3
  • You shouldn't have removed the JS from the example, anyway, try executing what is between the script tags, literally as it is. Most likely the error is escaping the strings, inside the html when you shouldn't. ie: $(\"body\") Commented Jan 25, 2014 at 22:47
  • I removed just </script> and everything worked as expected, it isn't a problem with escaping the html outisde the script or js inside the script Commented Jan 25, 2014 at 22:53
  • :l the stackoverflow highlighting tricked me! Commented Jan 25, 2014 at 22:57

1 Answer 1

4

If you use '</script>' your script will terminate. Then, you can use '</scr'+'ipt>'.

Demo


Note: the code above avoids errors, but doesn't execute the script.

If you want to execute it, you should use DOM methods:

var s = document.createElement('script');
s.text = "alert('a')";
document.body.appendChild(s);

Demo

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

1 Comment

This however doesn't evaluate what is inside the script

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.