I've got a script that inserts some content into an element using innerHTML
.
The content could for example be:
<script type="text/javascript">alert('test');</script>
<strong>test</strong>
Problem is that the code inside the <script>
tag doesn't get executed.
I googled it a bit but there were no apparent solutions. If I inserted the content using jQuery $(element).append(content);
the script parts got eval
'd before being injected into the DOM.
Has anyone got a snippet of code that executes all the <script>
elements? The jQuery code was a bit complex so I couldn't really figure out how it was done.
Edit:
By peeking into the jQuery code I've managed to figure out how jQuery does it, which resulted in the following code:
Demo:
<div id="element"></div>
<script type="text/javascript">
function insertAndExecute(id, text)
{
domelement = document.getElementById(id);
domelement.innerHTML = text;
var scripts = [];
ret = domelement.childNodes;
for ( var i = 0; ret[i]; i++ ) {
if ( scripts && nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
}
}
for(script in scripts)
{
evalScript(scripts[script]);
}
}
function nodeName( elem, name ) {
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
}
function evalScript( elem ) {
data = ( elem.text || elem.textContent || elem.innerHTML || "" );
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");
script.type = "text/javascript";
script.appendChild( document.createTextNode( data ) );
head.insertBefore( script, head.firstChild );
head.removeChild( script );
if ( elem.parentNode ) {
elem.parentNode.removeChild( elem );
}
}
insertAndExecute("element", "<scri"+"pt type='text/javascript'>document.write('This text should appear as well.')</scr"+"ipt><strong>this text should also be inserted.</strong>");
</script>
It's easier to use jquery
$(parent).html(code)
instead ofparent.innerHTML = code
:This also works with scripts that use
document.write
and scripts loaded viasrc
attribute. Unfortunately even this doesn't work with Google AdSense scripts.Try this, it works for me on Chrome, Safari & Firefox:
One thing to note though, is that the following div-nested script will NOT run:
For a script to run it has to be created as a node then appended as a child. You can even append a script inside a previously injected div & it will run (I've run into this before when trying to get ad server code to work):
Try function eval().
This is a real example from a project that i am developing. Thanks to this post
Here's a shorter, more efficient script that also works for scripts with the
src
property:Note: whilst
eval
may cause a security vulnerability if not used properly, it is much faster than creating a script tag on the fly.You should not use the innerHTML property but rather the appendChild method of the Node: a node in a document tree [HTML DOM]. This way you are able to later call your injected code.
Make sure that you understand that
node.innerHTML
is not the same asnode.appendChild
. You might want to spend some time on the Javascript Client Reference for more details and the DOM. Hope the following helps...Sample injection works:
regards,
Expending the answer of Lambder
You can use base64 image to create and load your script
Or if you have a
Iframe
you can use it instead