跨域JavaScript代码与同级子域 [英] Cross-domain JavaScript code with sibling sub-domains

查看:123
本文介绍了跨域JavaScript代码与同级子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个网络应用程序设置:



david .example.com和 john .example.com



David打开一个新窗口给John:

  window.open ('john.example.com'); 

John现在要在David上设置输入元素:

  $(#input,window.opener.document).val(Hello David。 

我的问题是,这并不适用于大多数现代浏览器,因为跨域脚本安全。我还尝试将 document.domain 设置为在David和John上的不同域组合,但未成功。



只有当John在 example.com 上并且都具有 document.domain 设置为 example.com 。但是, example.com 是我们的主要网站,不能作为解决方案。



我可以做上面的例子,同时使解决方案在所有现代的安全意识的浏览器中工作。



注意




  • 我控制 example.com 及其所有子网域。

  • >


解决方案 / div>

您应该能够这样做,只要在两个DOM上设置document.domain。

  document.domain = location.host.replace(/^.*?([^.++,[ ^。] +)$ / g,'$ 1'); 

贷记给Martin Jespersen


I have two web applications setups:

david.example.com and john.example.com

David opens a new window to John:

window.open('john.example.com');

John would now like to set an input element back on David:

$("#input", window.opener.document).val("Hello David.");

My problem is that this doesn't work on most modern browser because of cross-domain scripting security. I've also tried setting document.domain to different domain combinations both on David and John with no success.

The only time this does work is if John is on example.com and both have document.domain set to example.com. However, example.com is our main website and is not available as a solution.

So is there a way I can do the above example while making the solution works in all modern security conscious browsers?

Notes

  • I control example.com and all its sub domains.
  • David and John are separate web applications and cannot be hosted on the same sub-domain.

解决方案

You should be able to do this, as long as you set document.domain on both DOM's.

document.domain = location.host.replace(/^.*?([^.]+\.[^.]+)$/g,'$1');

Credited to Martin Jespersen

这篇关于跨域JavaScript代码与同级子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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