CodeBlue
CodeBlue

Reputation: 15389

How to send the value of a variable in one window to a new window using Javascript?

I have the following JavaScript code -

    function addOnClickEventHandler()
     {
      var userNameElement = document.getElementById("currentUser");
      var userName = userNameElement.value; // send this value to "chatWindow"

      window.open("chatWindow.html", "Chat Window", "resizable=0,width=700,height=600");
     }  

Now, how do I send the value of userName to "chatWindow"? Also, how to access this value once it has been sent?

Upvotes: 0

Views: 163

Answers (2)

Amritpal Singh
Amritpal Singh

Reputation: 1785

you can try

window.open("chatWindow.html?id=1", "Chat Window", "resizable=0,width=700,height=600");

 To Read Argument create js function in opening page and create following function

function getArgs() {
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split("&");
for (var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('=');
    if (pos == -1) continue;
    var argname = pairs[i].substring(0, pos);
    var value = pairs[i].substring(pos + 1);
    args[argname] = unescape(value);
}
return args;

} then use var args = getArgs();
var hid =args.id;

Upvotes: 0

DiningPhilanderer
DiningPhilanderer

Reputation: 2767

In the window being opened you can refer to the parent window as follows:

var oWinCaller = top.opener;

With this reference you can then get whatever you want from the opening window when the new window opens.

Upvotes: 1

Related Questions