簡體   English   中英

如何將數據從一個窗口發送到另一窗口?

[英]How to send data from one window to another window?

您能告訴我如何將數據從一個窗口發送到另一窗口嗎? 我的父窗口上有一個按鈕。 單擊該按鈕后,我將移至另一頁(另一頁中顯示成功按鈕)。 當我單擊子窗口或另一個窗口上的成功按鈕時,我將重定向到父窗口。 我想在父窗口上打印“成功”文本。

http://plnkr.co/edit/6IRDD0DHxK6A7mExWOUP?p=preview

$(function() {
  $('#f').on('click', function() {       
    // alert('ddd');
    window.open('a.html')
  })

   $('#succ').on('click', function() {        
    // alert('succ');
    window.open('index.html')
  })
})

只需執行以下操作:

在您的父頁面上添加以下代碼:

$(document).ready(function() {
   var referrer =  document.referrer;    //to get previous url
   if(referrer == 'a.html'){
       alert("success");    // here i am showing an alert box with success message, you can print it inside a div or span as per your need.
   }
});

注意:在Plunker上,用以下一項替換“ if condition”:

  if(referrer=='http://run.plnkr.co/Bwm0sBW4RRnrmWRA/a.html'){
       alert("success");    // here i am showing an alert box with success message, you can print it inside a div or span as per your need.
   }

並發送數據:

do something like this:
window.open('index.html','height=400, width=650, left=300, top=100, status=yes');
or
window.open("index.html?cid=hello");

而已。 :)

您可以通過url參數將值傳遞給html頁面。 只需在script.js文件中更改代碼即可。 希望對您有幫助。

$(function() {
  $('#f').on('click', function() {
    window.open('a.html?id=' + 123)
 })
 $('#succ').on('click', function() {
   window.open('index.html?id=' + 1)
 })
})

那么您可以從參數中獲取ID值。 謝謝

您可以使用localStorage API來跟蹤是否單擊了按鈕。

每當用戶單擊按鈕時,就向localStorage添加一個新鍵(例如isClicked)。

當用戶登陸主頁時,檢查是否設置了此標志。 如果設置,則顯示成功消息。

<!-- index.html -->
<html>
  <body>
    <div id="result"></div>
    <button id="cta">GOTO Sub Page</button>
    <button id="clear">Clear Success</button>
    <script>
      window.onload = function(){
        var isClicked = localStorage.getItem("isClicked");
        if(isClicked){
          document.getElementById("result").innerHTML="Success";
        }
      }
      document.getElementById("cta").onclick=function(){
       window.open("sub-page.html");
      }
      document.getElementById("clear").onclick=function(){
        localStorage.removeItem("isClicked");
        document.getElementById("result").innerHTML="";
      }
    </script>
  </body>
</html>

<!--- sub=page.html -->
<html>
  <body>
    <div id="result"></div>
    <button id="cta">Activate Message</button>
    <script>
      document.getElementById("cta").onclick=function(){
        localStorage.setItem("isClicked","true");
        window.open("index.html");
      }
    </script>
  </body>
</html>

示例Plunkr實現

PS:您也可以使用localStorage來存儲/檢索與頁面相關的其他值。 您甚至可以通過存儲JSON.stringify方法給出的字符串表示形式來存儲JS對象。

暫無
暫無

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

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