什么是Javascript中window.open()的返回类型

时间:2011-07-21 14:09:10

标签: javascript jquery ajax

我正在编写代码以使用window.open()下载PDF文件。我正在服务器上传递pdf文件的URL路径。

window.open(url, name, "width=910,height=750,scrollbars=yes");

我想检查文件下载是否成功。 window.open()的返回类型是什么?

我试过这个

try
{
  window.open(url, name, "width=910,height=750,scrollbars=yes");
  alert("success");
}
catch(e)
{
  alert("failer");
}

当我将网址更改为错误的网址时,会显示与成功相同的结果。

2 个答案:

答案 0 :(得分:7)

http://www.javascripter.net/faq/openinga.htm

  

返回值是对新窗口的引用。您可以使用   稍后此引用,例如,关闭此窗口   (winRef.close()),将焦点放在窗口(winRef.focus())或执行   其他窗口操作。

答案 1 :(得分:4)

Window.open要么返回打开的新窗口的句柄,要么返回null,它不会告诉您窗口中的页面是否成功加载。如果您打开一个html页面(来自同一个域),您可以使用它来查看文档

var newWin = window.open();
if(newWin == null) {
  alert("darn");
}
newWin.document.getElementById("anElement").innerText = "Fish";