0

Could you please tell me how to send data from one window to another window? I have one button on my parent window. On click of that button I am moving to another page (There is success button present in another page). When I click success button on child or another window I am redirecting to parent window. I want to print "success" text on parent 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')
  })
})
ankhzet
  • 2,517
  • 1
  • 24
  • 31
user944513
  • 12,247
  • 49
  • 168
  • 318

3 Answers3

0

Just do the following:

on your parent page add the following code:

$(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.
   }
});

NOTE: on Plunker replace "if condition" with this one:

  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.
   }

and to send data along with it:

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");

That's it. :)

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
0

You can pass value to the html page through url parameter. just change your code in script.js file. Hope it will help you.

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

then you can fetch the id value form params. Thank you

biswajit-rout
  • 377
  • 2
  • 11
0

You can use the localStorage API to keep track of whether the button was clicked or not.

Whenever the user clicks on the button, add a new key (isClicked, for example) to the localStorage.

When the user lands on the main page, check to see if this flag is set. If set, show the success message.

<!-- 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>

Sample Plunkr Implementation

PS: You can use localStorage to store/retrieve other page related values as well. You can even store JS objects by storing their string representations as given by JSON.stringify method

Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35