JavaScript:从弹出窗口获取值(外部网站) [英] JavaScript: GET value from popup (external website)

查看:127
本文介绍了JavaScript:从弹出窗口获取值(外部网站)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码从弹出框中的字段中获取值。

http://myotherwebsitename.com/parent.html

 <输入类型=textid =output/> 
< button id =show>打开< / button>

< script>
document.getElementById('show')。addEventListener('click',function(){
window ['output'] = document.getElementById('output');
window.open ('http://mywebsite.com/map.html')
});
< / script>

http://mywebsite.com/map.html

 < input type =textid = USER_TEXT/> 
< input id =sendtype ='button'value'send'/>

< script>
document.getElementById('send')。addEventListener('click',function(){
window.opener ['output']。value = document.getElementById('user_text').value;
})
< / script>

如果两者都托管在同一个网站上,效果很好,但如果我将其中一个放在其他网站上网站它不起作用。



我如何使它适用于不同的网站?



谢谢! / p>

解决方案

由于相同的原产地政策,您无法从其他网站获取信息。这里有一个更好的定义,说明为什么......在计算中,同源策略是许多浏览器端编程语言的重要安全概念,例如JavaScript的。该策略允许脚本在源自同一站点的页面上运行 - 方案,主机名和端口号[1]的组合 - 访问彼此的DOM没有特定的限制,但是阻止访问不同站点上的DOM [1]。同源策略也适用于XMLHttpRequest和robots.txt。



http://en.wikipedia.org/wiki/Same-origin_policy



希望这有助于您。我会看看我是否可以找到解决方法并通知您。


I'm using this code to get a value from a field in popup.

http://myotherwebsitename.com/parent.html

<input type="text" id="output"/>
<button id="show">Open</button>

<script>
    document.getElementById('show').addEventListener('click', function(){
        window['output'] = document.getElementById('output');
        window.open('http://mywebsite.com/map.html')
    });
</script>  

http://mywebsite.com/map.html

<input type="text" id="user_text"/>
<input id="send" type='button' value'send'/>

<script>
    document.getElementById('send').addEventListener('click', function(){
    window.opener['output'].value = document.getElementById('user_text').value;
})
</script>

It works well if both are hosted on the same website, but if i put one of these on other website it doesn't work.

How can i make it working for different websites?

Thanks!

解决方案

Due to same origin policy you can't get info from another website. here is a better definition of why...

In computing, the same-origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number[1] – to access each other's DOM with no specific restrictions, but prevents access to DOM on different sites.[1] Same-origin policy also applies to XMLHttpRequest and to robots.txt.

http://en.wikipedia.org/wiki/Same-origin_policy.

hope this helps. I'll see if I can find a workaround and let you know though.

这篇关于JavaScript:从弹出窗口获取值(外部网站)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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