简体   繁体   中英

How to pass arguments to a window using window.open?

I basically need to show some information about the current game in a separate window, and I used to do it like this:

gWindow = window.open("", "", "width = 350, height = 300, location = no");

if (self.pl.guild != undefined)
{
    gWindow.document.write("<p> Name: " + self.pl.guild.n + "</p>");
}

Now, I need that window to be a bit more complex... I'd like to have a pre-prepared page that I'll just feed into it, I know how to do that, but I'd like to give that window arguments, or have the window itself be able to read them from the main page. How do you do that?

The newly opened window has a property, which refers to the main window:

var main_window = window.opener;
alert(main_window.location.href); // Example

Propeties defined on the gWindow object at the main page will also be defined at the window (globally) at the opened window, because gWindow refers to the window object of the new window.

Pass them via the query line and parse them on the receiving side.

For example:

window.open("myotherpage.html#param1=X|param2=Y" ...

And inside myotherpage.html, do:

var params = window.location.href.substring(window.location.href.indexOf('#')+1);
params = params.split('|');
for(var i=0; i<params.length; i++) {
  var pair = params[i].split('=');
  var key = pair[0];
  var value = pair[1];

  // .. your code here ..

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM