子域iframe内容访问代码在Chrome中无效

Exc*_*ion 1 iframe jquery

AFAIK我们无法访问从不同域加载的iframe的DOM内容.但在我的情况下,我有一个名称如下的网站

 http://dom1.myMainDomain.com/******  
Run Code Online (Sandbox Code Playgroud)

在该页面中,我将一个来自子域的HTML文件加载到上述网站中的Iframe,该网站是如上所述的子域.即

 http://dom2.myMainDomain.com/******/1.html
Run Code Online (Sandbox Code Playgroud)

我试过下面的代码,但它不起作用

 $(document).ready(function () {
    var str = "http://dom1.myMainDomain.com/*****Images/";
    $('body').append('<iframe id="ifr" style="position:absolute;top:0px;left:0px;width:100%;height:100%" src="' + str + $('#htmNum').val() + '.html"></iframe>');
    document.domain = "myMainDomain.com";
    $('#ifr').load(function () {
        $('#ifr').contents().find('body').html('Hey, i`ve changed content of <body>! Yay!!!');
    });
});
Run Code Online (Sandbox Code Playgroud)

但它对我不起作用..我嵌入上述代码的网站是

http://dom2.myMainDomain.com/*****.aspx    
Run Code Online (Sandbox Code Playgroud)

Please help me on this. As per the answer I have tried and it was working in IE7./ But in Chrome I can see permission denied message

Vik*_* S. 5

你必须document.domain = "myMainDomain.com";在paretnt页面和这里做http://dom1.myMainDomain.com/*****Images/.您的文档就绪功能正常,但您也应该在此处使用类似的代码http://dom1.myMainDomain.com/*****Images/.我会把它放在头部像

<head>
<script type="text/javscript">
   document.domain = "myMainDomain.com";
</script>
</head>
Run Code Online (Sandbox Code Playgroud)

并且假设将相同的代码移到父文档的头部会更好.只是为了确保在访问iframe时正确设置了所有内容

是啊.只需仔细检查一下.我可以使用以下代码访问iframe内容:

$("iframe").contents().find("body")
Run Code Online (Sandbox Code Playgroud)

如果在父窗口和iframe中都有document.domain = "com.local";javascript