2014-10-03 94 views
1

我希望能够在我的网站上创建一个弹出式窗口,用户可以从该窗口在网站上打印几张图像。我为此编写的代码似乎适用于Chrome和Firefox,但由于某些原因,代码无法写入IE 11中的页面。例如,下面的代码在Chrome中工作,但只是在IE 11中创建一个空白页面。有什么方法可以改变它,所以它可以在IE中工作?IE中的新窗口是空白的

<button id="sub" onclick="printme(event)">Submit</button> 
<script> 
function printme(evt) 
{ 

    link = "about:blank"; 
    var pw = window.open(link, "_new"); 
    pw.document.open(); 
    pw.document.write("There should be writing here"); 
    pw.document.close(); 
} 
</script> 
+1

是否有在JavaScript控制台中的任何错误?如果你使用'link =“”;'而不是'about:blank',它会起作用吗? – Barmar 2014-10-03 04:32:01

+0

您的代码在IE 11中可以正常工作。 – pkExec 2014-10-03 04:34:22

+0

也适用于我 – 2014-10-03 04:37:21

回答

0

试试这个

window.onload=function() { 
    document.getElementById("sub").onclick=function() { 
    var link = ""; 
    var pw = window.open(link, "_new"); 
    if (pw) { // can be blocked or not there for timing reasons 
     pw.document.write("There should be writing here"); 
     pw.document.close(); 
    } 
    else { 
     alert("Please disable your popup blocker"); 
    } 
    return !pw; // return false if successful 
    } 
} 

这将工作更好地与

<a href="popupblocked.html" id="sub">Submit</a> 
+0

不幸的是,这段代码仍然不适合我。 IE能够打开窗口,但错误是抛出document.write – 2014-10-04 03:07:02

+0

我错过了一个括号。你能用你的实际代码创建一个jsfiddle吗?例如,如果你尝试document.write('',你将失败,如果没有逃脱 – mplungjan 2014-10-04 06:12:46

+1

我知道它不必与我的实际代码,因为上面的代码在文档中没有标签。 write()仍然不起作用,最后我意识到我和其他网站有完全相同的问题,例如,在IE中,点击“print”后我看不到打印预览:http:/ /www.coloring.ws/t.asp?b=m&t=http://www.coloring.ws/canada/carr-forest.gif,所以我认为这个问题与我的安全设置有关。 – 2014-10-08 01:30:03