2010-12-07 89 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>