如何在window.write()中编写js

时间:2016-06-24 01:52:58

标签: javascript php html window.open

此javascript函数打开一个新窗口,显示数据sais(数据包含图像路径)的图像。我想在用户点击它的任何地方时关闭这个新窗口。我以为我可以通过在window.write()中插入一些javascript来做到这一点,但我做错了。任何提示?

function myFunction(data) {
    img = new Image();
    img.src = data;
    window.open(data, '_blank', 'toolbar=0,location=0,menubar=0, width = '+ img.width +', height=' + img.height);
    //window.write(<script type="text/javascript"> $(document).click(function(e) { if (e.button == 0) {window.close(); }}); </script>);
}

1 个答案:

答案 0 :(得分:1)

您将无法将JavaScript添加到仅包含图像的窗口。最好的办法是为每个图像创建一个HTML页面,然后打开一个空白窗口,并通过操作窗口的文档来添加图像和脚本。

如果你走第二条路线,你需要为窗口制作一个变量:

var mywindow = window.open('', '_blank', …);
mywindow.document.…