Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add script files in child window using javascript?

Tags:

javascript

How to add script files in child window using javascript?

Consider the following code:

myWindow = window.open("", "", 'width=650,height=700,menubar=yes,resizable=yes,scrollbars=yes');
myWindow.focus();
myWindow.document.write('<script src="'+App.data.assets_url+'\/javascript\/jquery.js"><\/script>');

Above code doesn't work properly in IE. It shows blank (child) window, but in chrome it works properly. It shows all the content of the child window.

In Mozilla it's not working properly too because of above myWindow.document.write line browser's print option.

like image 606
xyz Avatar asked Oct 26 '25 15:10

xyz


2 Answers

Basically:

var win, doc;

win = window.open('', 'dialog', opts);
doc = win.document;

doc.write(
    "<html><head>"
    + "<script type='text/javascript' src='path/to/your/script.js'></script>"
    + "<script type='text/javascript'>"
    + "/* this is inline script inserted by JavaScript, below is a function converted to it's string representation */"
    + someFuncInVariable.toString()
    + "</script>"
    + "</head><body>"
    + "</body></html>"
);
doc.close();
like image 146
jkrcma Avatar answered Oct 29 '25 05:10

jkrcma


Supposing you don't have domain crossing, you may simply do this (using jquery) :

$(childwindow.document.body).append('<script src="..."></script>');

But a more detailed question might enable more on topic answers.

like image 39
Denys Séguret Avatar answered Oct 29 '25 05:10

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!