簡體   English   中英

在不同頁面的輸入框之間發送數據

[英]Send data between input boxes in different pages

我想制作一個輸入框,將其通過按鈕等將數據發送到新選項卡(和新頁面)中的另一個輸入框。我進行了很多搜索(URL數據傳輸&...),但仍然不知道。 我感謝任何起點或觀點。 謝謝 ! 編輯:它們是具有不同域的不同站點。 編輯2:我無權編輯收件人頁面。

您有數百萬種選擇。

您可以傳遞搜索參數,哈希,會話存儲...

發件人小提琴

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

接收器小提琴 (無效,但已給出實現)

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

這里使用搜索參數方法。 但正如我所說,您有很多選擇。

直到我了解更多為止,我會說localstorage是您在這里需要的:

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

因此,一個窗口保存數據(使用JSON.stringify ),而另一個窗口偵聽storage事件並使用JSON.parse解析該數據。 同樣,兩個窗口都可以一起發送和接收。

此處還有更多信息-https: //developer.mozilla.org/zh-CN/docs/Web/Guide/API/DOM/Storage

還有2個標簽的演示頁面-http: //html5demos.com/storage-events

對於跨域消息傳遞,我建議使用postMessage

瀏覽器支持足夠公平,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);

上面的示例代碼摘自David Walsh的這篇文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM