简体   繁体   English

在不同页面的输入框之间发送数据

[英]Send data between input boxes in different pages

I would like to make an inputbox which sends the data to another inputbox in a new tab ( and a new page ) with a button or etc. I searched for it a lot ( url data transfer & ... ) but still have no idea . 我想制作一个输入框,将其通过按钮等将数据发送到新选项卡(和新页面)中的另一个输入框。我进行了很多搜索(URL数据传输&...),但仍然不知道。 I appreciate any start point or sth. 我感谢任何起点或观点。 Thanks ! 谢谢 ! Edit : they are different sites with different domains. 编辑:它们是具有不同域的不同站点。 Edit 2 : I don't have any accesses to edit the receiver page. 编辑2:我无权编辑收件人页面。

You have millions of options. 您有数百万种选择。

You can pass with search parameters , hash, session storage... 您可以传递搜索参数,哈希,会话存储...

Sender Fiddle 发件人小提琴

$('#send').on('click', function(e) { 
    window.open('http://jsfiddle.net/cnckfzpy/3/' + "?data=" + $("#sen").val(), "_blank")
});

Receiver Fiddle (Doesn't work, but the implementation is given) 接收器小提琴 (无效,但已给出实现)

$('#rec').val(window.location.search.split('data=')[1]);

Here is used the search parameter approach. 这里使用搜索参数方法。 but as I said, you have many options to choose from. 但正如我所说,您有很多选择。

until I know more, I would say localstorage is what you need here: 直到我了解更多为止,我会说localstorage是您在这里需要的:

http://www.codediesel.com/javascript/sharing-messages-and-data-across-windows-using-localstorage/ http://www.codediesel.com/javascript/sharing-messages-and-data-across-windows-using-localstorage/

So one window saves the data (using JSON.stringify ) and the other window listens to the storage event and parsing that data using JSON.parse . 因此,一个窗口保存数据(使用JSON.stringify ),而另一个窗口侦听storage事件并使用JSON.parse解析该数据。 Also, both window can send and receive together. 同样,两个窗口都可以一起发送和接收。

And more info here - https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage 此处还有更多信息-https: //developer.mozilla.org/zh-CN/docs/Web/Guide/API/DOM/Storage

And a demo page for 2 tabs - http://html5demos.com/storage-events 还有2个标签的演示页面-http: //html5demos.com/storage-events

For cross-domain messaging I would suggest utilizing postMessage . 对于跨域消息传递,我建议使用postMessage

Browser support is fair enough, and the API is very very easy: 浏览器支持足够公平,API非常简单:

//create popup window
var domain = 'http://scriptandstyle.com';
var myPopup = window.open(domain + '/windowPostMessageListener.html','myWindow');

//periodical message sender
setInterval(function(){
    var message = 'Hello!  The time is: ' + (new Date().getTime());
    console.log('blog.local:  sending message:  ' + message);
    myPopup.postMessage(message,domain); //send the message and target URI
},6000);

//listen to holla back
window.addEventListener('message',function(event) {
    if(event.origin !== 'http://scriptandstyle.com') return;
    console.log('received response:  ',event.data);
},false);

The above example code was taken from this article by David Walsh. 上面的示例代码摘自David Walsh的这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM