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

佐手、 提交于 2019-12-02 18:33:37
vivek

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.

vivek

I hope this code help you

This should be in home page:

function sample(id) {
    sessionStorage.setItem("sent", id);
    window.open("result.html","_blank");
}

This is another way in the same home page function:

function sample() {
    var id=document.getElementById("your_required_id").id;
    sessionStorage.setItem("sent", id);
    window.open("result.html","_blank");
}

This should be in result page:

function sample1() {
    var a=sessionStorage.getItem("sent");
    alert(a);
}

The id may be your text box id

In result.html, find the Window which opened it, using window.opener and then take your data of interest from that Window.

window.addEventListener('load', function () { // wait for ready
    var home = window.opener, txtinput, txtresult;
    if (home) {
         txtinput = home.document.getElementById("txtinput");
         txtresult = document.getElementById('txtresult');
         txtresult.value = txtinput.value;
    }
}, false);

In home.html, listen for a click on #btn and open result.html

// assuming button exists at invocation time
var btn = document.getElementById('btn');
btn.addEventListener('click', function () {
    window.open('result.html');
}, false);

i think that a simple assignment, using the window.opener handle from within the child window, is what you need:

if (window.opener) document.getElementById("#txtresult").value = window.opener.document.getElementById("#txtinput").value;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!