2011-07-21 35 views
5

我正在編寫使用window.open()下載PDF文件的代碼。我在服務器上傳遞pdf文件的URL路徑。JavaScript中的window.open()的返回類型是什麼

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"); 
} 

當我更改URL到錯誤的URL,它顯示了同樣的結果是成功的。

+0

你不檢查任何返回值。 'open()'返回'window reference'或'null'。 – jAndy

回答

7

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

返回值是引用您的新窗口。例如,您可以稍後使用此參考文件 關閉此窗口 (winRef.close()),將焦點置於窗口(winRef.focus())或執行其他窗口操作。

4

Window.open要麼返回一個處理到新窗口打開或爲空,它不會告訴你,如果窗口內的頁面加載成功。如果你在哪裏打開一個html頁面(來自同一個域),你可以用它來查看文檔

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