user3023421
user3023421

Reputation: 363

Returning a JavaScript function called from JavaScript function error

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

Upvotes: 0

Views: 50

Answers (1)

Oriol
Oriol

Reputation: 288020

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

Upvotes: 4

Related Questions