JQuery iframe 内容跨域 [英] JQuery iframe contents cross-domain

查看:60
本文介绍了JQuery iframe 内容跨域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 iframe 源位于不同的域时,是否可以使用 jQuery 访问 iframe 的内容?我不想修改内容,只是在 iframe 完成后读入 html,所以这就是为什么我不确定这是否属于 同源策略.

Is it possible to use jQuery to access the contents of an iframe when the iframe source is on a different domain? I'm not looking to modify the contents, just read in the html when the iframe is finished, so that's why I'm not sure if this falls under the Same Origin Policy.

例如:

域:http://www.example-A.com/

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $("#helper").append("<iframe src='http://www.citizensbank.com' ></iframe>");
        getContents("#helper");
    });
    function getContents(iframeID) {
        if ($(iframeID + " iframe").context.readyState != "complete") {
            setTimeout("getContents('" + iframeID + "');", 200);
        } else {
            alert($(iframeID + " iframe").contents().html());
        }
    }
</script>
<div id="helper"><iframe src="http://www.example-B"></iframe></div>

推荐答案

不,如果 iframe 的 src 与父页面的域不同,则无法使用 javascript 读取 iframe 的内容.

No, it is not possible to read the contents of an iframe using javascript if the iframe's src is a different domain from the parent page.

这篇关于JQuery iframe 内容跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆