Reputation: 168101
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?
Upvotes: 0
Views: 87
Reputation: 160843
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>');
Upvotes: 3
Reputation: 2757
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>
Upvotes: 4