[英]Send data between input boxes in different pages
我想制作一個輸入框,將其通過按鈕等將數據發送到新選項卡(和新頁面)中的另一個輸入框。我進行了很多搜索(URL數據傳輸&...),但仍然不知道。 我感謝任何起點或觀點。 謝謝 ! 編輯:它們是具有不同域的不同站點。 編輯2:我無權編輯收件人頁面。
直到我了解更多為止,我會說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.