【问题标题】:Does setting document.domain work in all (most) browsers?设置 document.domain 是否适用于所有(大多数)浏览器?
【发布时间】:2013-08-23 06:41:34
【问题描述】:

Same Origin Policy Documentation 是这样说的:

同样有一个例外 原产地规则。一个脚本可以设置 document.domain 到后缀的值 当前域的。如果这样做, 较短的域用于 随后的原产地检查。例如, 假设文档中的脚本位于 http://store.company.com/dir/other.html 执行以下语句:

document.domain = "company.com";

之后 该语句执行,页面 将通过原产地检查 http://company.com/dir/page.html。 然而,同样的道理, company.com 无法设置 document.domain 到 othercompany.com。

所有流行的浏览器都支持这个吗?如果没有,哪些没有?

【问题讨论】:

    标签: javascript browser cross-browser


    【解决方案1】:

    Firefox 2,3, IE6,7,8, Chrome, Safari 2 和 3, Opera 9 都支持 document.domain;

    其他“较新”的浏览器也可能会,但这些是我实际测试过我的代码(使用 document.domain)的浏览器

    【讨论】:

    • @Alan:感谢您提供的信息。如果可以分享,您这样做的原因是什么?我有兴趣让向不同机器发出 AJAX 类型的请求更容易,但我很好奇是否还有其他用途。
    • 切记在IE6-7中设置document.domain="string of the original page page was on" and document.domain=document.domain导致caos
    【解决方案2】:

    文档域要小写,规则是这样的

    // Actual domain is "www.foo.com" 
    document.domain = "foo.com"; // this is valid 
    
    // Actual domain is "bar.foo.com" 
    document.domain = "www.foo.com"; // this is invalid, "bar.foo.com" is not a subdomain of "www.foo.com" 
    
    // Actual domain is "blah.bar.foo.com" 
    document.domain = "bar.foo.com" // Ok 
    document.domain = "foo.com" // Still ok 
    document.domain = "bar.foo.com" // Invalid, you can't change it back to a more specific domain.
    

    【讨论】:

    • 那么我应该在托管在不同域的服务器和应用程序中添加 document.domain="sample.com" 吗?
    猜你喜欢
    • 2013-08-02
    • 2012-03-15
    • 2017-07-09
    • 2010-11-04
    • 2015-05-15
    • 1970-01-01
    • 2013-09-21
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多