如何使用javascript在子窗口中添加脚本文件

How to add script files in child window using javascript?

本文关键字:添加 脚本 文件 窗口 何使用 javascript      更新时间:2023-09-26

如何使用javascript在子窗口中添加脚本文件?

请考虑以下代码:

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>');

上面的代码在IE中无法正常工作。它显示空白(子)窗口,但在 chrome 中它工作正常。它显示子窗口的所有内容。

在Mozilla中,由于上面myWindow.document.write行浏览器的打印选项,它也无法正常工作。

基本上:

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();

假设你没有域交叉,你可以简单地这样做(使用jquery):

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

但是,更详细的问题可能会提供更多的主题答案。