2011-08-25 95 views
0

當前我正在使用JavaScript和Ajax來獲取一些數據並將其呈現在新窗口中。我試圖在打開一個新窗口之前關閉OpenFileWindow()中的窗口,但是當它找到窗口對象時,所有的屬性和方法都會給出一個權限被拒絕的錯誤。關閉XMLHttpRequest處理程序中的現有打開的窗口

我相信它與Ajax調用的範圍有關,因爲當我在XMLHttpRequest之前打開窗口時,沒有問題。

我不知道如何繼續,我搜索了很多。任何人有任何建議?謝謝。

打開

var newWin = null; 
function View_onClick(propId, url) { 
    var param = "propId=" + propId; 
    param += "&filename=" + url; 
    var xhr = new XMLHttpRequest(); 
    xhr.open("POST", "GetActivityFileName.ashx", false); 
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    xhr.onreadystatechange = function() { 
     if (xhr.readyState == 4) { 
      if (xhr.status == 200) { 
       if (xhr.responseText == "") { 
        alert("Sorry unable to find file."); 
        return false; 
       } 
       else { 
        OpenFileWindow(xhr.responseText) 
        return false; 
       } 
      } 
     } 
    } 
    xhr.send(param); 
    return false; 
} 


function OpenFileWindow(fileUrl) { 
     if(newWin != null) 
      newWin.close(); 
     newWin = window.open(fileUrl); 
     newWin.focus(); 
} 

回答

0

如果你的意圖是剛剛重新使用窗口,爲什麼不命名。

如果您給出與第二個參數window.open,相同的名稱,它將重新使用該窗口。

+0

其實我試過這個,我用window.open的第二個參數,但它仍然不斷打開新的窗口。我相信這是一個範圍問題,因爲當我在Ajax調用之前打開一個URL時,那麼這是有效的。 – JLi

0

這個怎麼樣。如果窗口仍然打開,請更改URL。否則,打開窗口並加載URL。

function OpenFileWindow(fileUrl) { 
     if(newWin == null || newWin.closed) 
      newWin = window.open(fileUrl); 
     else 
      newWin.location.replace(fileUrl); 

     newWin.focus(); 
} 
+0

newWin.location.replace(fileUrl)仍然給予權限被拒絕。 – JLi

+0

fileURL在不同的域上? – James

+0

是的,它位於不同的域。這是我必須肯定檢查出來的,因爲之前沒有XMLHttpRequest的代碼正在工作,它允許關閉窗口。 – JLi