2012-06-25 22 views
0

我有一個帶有Google文檔和下載鏈接的頁面(下面是人爲示例)。當鏈接被點擊時,iframe變灰,並且根本不會回來。我已經把頁面剝下來,所以它只是這兩個元素,它仍然會發生。當任何下載鏈接被點擊時,iframe中的Google文檔變灰色

這肯定發生在Firefox,Chrome和Safari中。

不包含Google文檔的Iframe似乎沒有此問題。

任何想法?

<html> 
<head></head> 
<body> 
<iframe src="http://docs.google.com/document/d/1mCYGLa8-Qsz_nYdk_f_8YefqWKMyulwVl223rebRMqM/preview" width="660" height="648"></iframe> 
<a class="download_link" href="http://fastdl.mongodb.org/osx/mongodb-osx-i386-2.0.6.tgz">Download</a> 
</body> 
</html> 

回答

0

有點討厭的解決方案是在鏈接標籤上設置target='_blank'。這會在單獨的窗口中打開請求,但是一旦請求被註冊爲下載,窗口將關閉,並將用戶留在原始頁面上,並在後臺下載新的下載。

這似乎在Chrome和Firefox上運行良好,但Safari會在下載完成時離開新窗口。

另一種解決方法是重新加載iframe設置,將其src屬性設置爲自身。唯一的問題是用戶將失去滾動位置。

我選擇了混合解決方案,但確實假設所有版本的Chrome和Firefox都顯示相同的行爲(在下載開始時關閉標籤)。這可能是不正確的,所以瀏覽器檢測應該可能會被改進。

或者,嘿,谷歌能解決谷歌文檔:)

 // preserve the google doc in the iframe. else the iframe goes gray when a download link is clicked 
     if (navigator.userAgent.indexOf('Chrome') != -1 || navigator.userAgent.indexOf('Firefox') != -1){ 
      // Chrome and Firefox open new tabs with target='_blank' but close them again when the download begins 
      // the page itself is unaffected 
      $('a.download_link').attr('target', '_blank'); 
     } 
     else{ 
      // the above doesn't work on Safari and probably others so fall back to reloading the iframe contents 
      $('a.download_link').live('click', function(){ 
       $('iframe').each(function(id, frame){ 
        frame.src = frame.src; 
       }); 
      }); 
     }