如果我使用www,我可以从子域访问iframe的内容吗?在主域? [英] Can I access contents of an iframe from a subdomain if I use www. on the main domain?

查看:144
本文介绍了如果我使用www,我可以从子域访问iframe的内容吗?在主域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问和编辑iframe的内容。 iframe指向我自己域上子域的页面。这是我正在使用的javascript代码,虽然它并不重要。

I'm trying to access and edit the contents of an iframe. The iframe points to a page on a subdomain on my own domain. This is the javascript code I'm using, although it doesn't really matter.

$('iframe').load(function(){
  $('div.code textarea.html').val($(this).contents()[0].html());
});

当我运行它时,它说我没有权限从www访问example.domain.com .domain.com。如果我尝试访问的域名具有www,那么重要吗?因为我的主持人不允许我不使用www。

When I run it it says I don't have permission to access example.domain.com from www.domain.com. So does it matter if the domain I try to access from has the www? Because my host does not allow me to not use www.

推荐答案

Same-Origin-Policy默认需要完全相同的主机名。

Same-Origin-Policy requires the exact same hostname by default.

要告诉它不要,请设置:

To tell it not to, set:

document.domain= 'domain.com';

来自父(www。)文档和iframe(示例。)文档中的脚本。

from script in both the parent (www.) document and the iframe (example.) document.

请注意,从脚本设置静态编写的iframe(或图像)的onload是不可靠的,因为可以想象iframe可能会在时间之间完全加载解析器读取iframe标记,并在读取设置onload的脚本标记时。

Note that setting an onload of a statically-written iframe (or image) from script is unreliable, as it is conceivable that the iframe might get completely loaded in between the time the parser reads the iframe tag and when it reads the script tag that sets the onload.

要避免这种情况,请将事件处理程序包含为内联'< iframe onload = doSomething()'属性(内联事件处理具有某些目的的少数几个地方之一),或者,如果可访问性可接受,则从脚本创建iframe元素,在写入src之前设置onload并将其添加到页面。

To avoid this, either include the event handler as an inline ‘<iframe onload="doSomething()"’ attribute (one of the few places where inline event handling has some purpose), or, if acceptable for accessibility, create the iframe element itself from script, setting the onload before writing the src and adding it to the page.

这篇关于如果我使用www,我可以从子域访问iframe的内容吗?在主域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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