Reputation: 23591
I'm trying to append javascript to a loaded page by:
var sc = document.createElement('script');
sc.content = "<script>alert('aa')</script>";
document.body.appendChild (sc);
But that didn't work in chromium:
Upvotes: 0
Views: 601
Reputation: 40639
var script = document.createElement('script');
script.appendChild(document.createTextNode("alert('http://jsfiddle.net/UpWhX/')"));
document.body.appendChild(script);
Try this TestHere http://jsfiddle.net/UpWhX/2/
Upvotes: 0
Reputation: 1075765
You don't put <script>...</script>
inside the content of a script
element. But more to the point, I don't think they have a content
property (I don't see one here, for instance).
If I recall correctly, the most reliable way to put content within a script
element at runtime is via createTextNode
and appendChild
, e.g.: Live example | source
var sc = document.createElement('script');
sc.appendChild(document.createTextNode("alert('aa')"));
document.body.appendChild(sc);
...but I wouldn't be surprised to find that some older browsers required you to set innerText
or something instead.
Upvotes: 3