2010-12-07 87 views
12

奇怪的是,似乎無法將Google文檔鏈接設置爲在新窗口中打開。 (目標= 「_空白」)。強制iFrame鏈接(在嵌入的Google Doc中)在新窗口中打開

當發佈谷歌文檔和使用嵌入功能,生成一個iframe代碼片段:

<iframe src="https://docs.google.com/document/pub?id=1mfSz_3cWh6eW-X3EhQTtCoZ33Km131An8Kyvmuxi5oM&amp;embedded=true"></iframe> 

文檔中的所有鏈接將內嵌框架中打開,並通過谷歌的重定向服務重定向: http://www.google.com/url?q=

有什麼辦法可以讓這些鏈接在新窗口中打開?我知道可能會出現跨框架腳本問題,所以很奇怪谷歌沒有簡單的方法來實現這一點...

回答

3

好吧,缺乏更好的替代方案,我決定先捲曲Google Doc URL,然後做一些jQuery魔術將其加載到iFrame中。

Curl.php

curl_setopt($ch, CURLOPT_URL, $Url); 
[...] 
$("#header").hide() 
$("#footer").hide() 
$('a[href^="http://"]').attr("target", "_blank"); 

page.html中

$("#google_content").html("<iframe width='100%' height='600' frameborder='0' src='http://www.example.com/Curl/Curl.php'></iframe>"); 

谷歌,真的是這樣建議的解決方法? ;)

+0

我知道這已經很長一段時間了,但是這是您得到這個工作的唯一方法嗎? – Matt 2014-01-24 19:44:48

+0

不幸的是 - 但今天有希望有更好的解決方案!? – dani 2014-01-25 20:41:41

3

用戶avioing鏈接到GitHub的要點:https://gist.github.com/psjinx/1f2317a50eb2b506ed84

這是一個很好的起點。

但是 - iframe srcdoc - 不支持在IE瀏覽器 - http://caniuse.com/#feat=iframe-srcdoc

我略微修改的方案,風格是可選的。

<style> 
    body { margin: 0; padding: 0; } 
    iframe { margin-left: 2vw; margin-top: 2vh; height: 90vh; width: 90vw; } 
</style> 


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 

<iframe srcdoc="" frameborder="0" scrolling="no"></iframe> 

<script> 
    $(function() { 
     $.get("https://docs.google.com/document/d/1WUsQ_Kaa_tJljadPpHO2AFwvOAIqrYFS_zehUd6iCVk/pub?embedded=true", function(html) { 
      var contents = $("iframe").contents(); 

      contents.find("html").html(html); 

      setTimeout(function() { 
       contents.find('a[href^="http://"]').attr("target", "_blank"); 
       contents.find('a[href^="https://"]').attr("target", "_blank"); 
      }, 1000); // Actually not sure if timeout is required here... 
     }); 
    }); 
</script>