How to pass a value from a parent window to another html page using javascript?

locknies picture locknies · Nov 26, 2013 · Viewed 36.7k times · Source

I have 2 windows home.html and result.html.

In home.html I have a <textarea> #txtinput and a <button> #btn.

In result.html I have another <textarea> #txtresult.

On home.html, if I enter a value into #txtinput and click #btn, I want to open result.html and pass the value of #txtinput into #txtresult.

I've tried the below code from another post, which displays the value in the new window's body but won't display it in my element

var myWindow = window.open();
myWindow.document.body.innerHTML = document.getElementById("txtinput").value;

Is it somehow possible in a simple way? I am relatively new to JavaScript, my courses are ongoing now and I am just curious to know the ways to do it. Any detailed help will be very much appreciated!

Answer

vivek picture vivek · Nov 27, 2013

I hope i need to elaborate the below code

Button on click function in the home page:

function sample(){
    //this will set the text box id to var id;
    var id = document.getElementById("text_box_id").id;

    //the sessionStorage.setItem(); is the predefined function in javascript
    //which will support for every browser that will store the sessions.
    sessionStorage.setItem("sent", id); 

    //this is to open a window in new tab
    window.open("result.html","_blank");
}

Retrieve the value in result page:

$(document).ready(function(){
    //This sessionStorage.getItem(); is also a predefined function in javascript
    //will retrieve session and get the value;
    var a = sessionStorage.getItem("sent");
    alert(a);
});     

For more information about sessionStorage

I have done same thing as above, am getting values in new window that's great, but that values I am getting only in documet.ready() function. So I am not able to use these values in my JSP. once I got values I need to display them in JSP.