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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6777396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:49:50  来源:igfitidea点击:

what is return type of window.open() in Javascript

javascriptjqueryajax

提问by Bhoomin yadav

I am writing code to download a PDF file using window.open(). I am passing the URL path of the pdf file on a server.

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

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

I want to check if the downloading of file is successful or not. What is the return type of window.open()?

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

I tried like this

我试过这样

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

When I change the URL to a wrong URL, it shows the same result as a success.

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

回答by Jakub Konecki

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

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

The return value is the reference to your new window. You can use this reference later, for example, to close this window (winRef.close()), give focus to the window (winRef.focus()) or perform other window manipulations.

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

回答by David Waters

Window.open either returns a handle to the new window opened or null, it will not tell you if the page within the window loaded successfully. If you where opening an html page (from the same domain) you could use this to look into the document

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

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