0

I am trying to attach a <script> node with the following code

<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
  $('body').append('<script>alert(\'foo\');</script>');
</script>
</body>
</html>

I expect the code

alert('foo');

to be added an executed, but actually, the following string is added

');

What is happening here?

2

2 Answers 2

4

You have to properly escape the slash character here and use doublequotes for foo:

<html>
  <body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>
      $('body').append('<script>alert("foo");<\/script>');
    </script>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

3

You have to break </script> inside the string or it will considered as the end tag for <script>.

  $('body').append('<script>alert(\'foo\');</scr'+'ipt>');

Comments

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.