Warning: loading the script this way will make the scripts work in the context of the main window ie: If you use a window from somescript.js, it will NOT be an iframe window!
$('#iframe').load(function() { $(this).contents().find('body').append('<scr' + 'ipt type="text/javascript" src="somescript.js"></scr' + 'ipt>'); });
To use an iframe context, insert the script as follows:
function insertScript(doc, target, src, callback) { var s = doc.createElement("script"); s.type = "text/javascript"; if(callback) { if (s.readyState){ //IE s.onreadystatechange = function(){ if (s.readyState == "loaded" || s.readyState == "complete"){ s.onreadystatechange = null; callback(); } }; } else { //Others s.onload = function(){ callback(); }; } } s.src = src; target.appendChild(s); } var elFrame = document.getElementById('#iframe'); $(elFrame).load(function(){ var context = this.contentDocument; var frameHead = context.getElementsByTagName('head').item(0); insertScript(context, frameHead, '/js/somescript.js'); }
Andriy F.
source share