2013-08-12 59 views
0

我在父窗口中有一個iframe和一個後退按鈕。我希望後退按鈕與瀏覽器歷史記錄按鈕完全相同。問題在於跨域。如果我點擊打開一個不同的域名頁面,那麼我的代碼會拋出錯誤。針對ContentWindow錯誤使用Jquery進行跨域錯誤處理

這裏是我的代碼

## Initialization code ## 
window.iFrameChanges = -1; //will get incremented to 0 on first page load 


## iframe on load event ## 
function iframeOnLoad() { 
window.iFrameChanges+=1; 
} 

## Back button - click event ## 
if(window.iFrameChanges > 1) { 
document.getElementById("show-file").contentWindow.history.go(-1); 
}else { 
window.iFrameChanges = -1; 
} 

錯誤

Error: Permission denied to access property 'history' document.getElementById("show-file").contentWindow.history.go(-1);

不是找這個一個確切的解決方案,我知道的跨域問題(另一方面,如果有任何溶膠,請讓我知道)。我只是想正確處理這些錯誤使用jQuery,以便有更好的最終用戶體驗。

請諮詢

回答

0

通過添加一個嘗試捕捉周圍的代碼

try { 
document.getElementById("show-file").contentWindow.history.go(-1); 
window.iFrameChanges -= 1; 
} 
catch(err) { 
window.iFrameChanges = -1; 

    ## code to show message to user (if required) ##    
} 
解決