强制iFrame链接(在嵌入式Google文档中)在新窗口中打开

dan*_*ani 12 html iframe google-docs hyperlink cross-domain

非常奇怪的是,似乎无法在新窗口中设置Google文档链接.(目标= "_空白").

发布Google文档并使用嵌入功能时,会生成iframe代码段:

<iframe src="https://docs.google.com/document/pub?id=1mfSz_3cWh6eW-X3EhQTtCoZ33Km131An8Kyvmuxi5oM&amp;embedded=true"></iframe>
Run Code Online (Sandbox Code Playgroud)

文档中的所有链接都将在iFrame中打开,并通过google的重定向服务重定向:http: //www.google.com/url?q =

有什么办法可以在新窗口中打开这些链接吗?我知道可能存在跨框架脚本问题,所以很奇怪谷歌没有简单的方法来实现这个目标......

dan*_*ani 5

好吧,由于缺乏更好的替代方案,我决定在将其加载到iFrame之前使用Curl Google Doc URL并执行一些jQuery魔术.

Curl.php

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

page.html中

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

谷歌,这真的是推荐的解决方法吗?;)


Mar*_*son 5

用户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>
Run Code Online (Sandbox Code Playgroud)