PDA

View Full Version : Passing parameters to a new window.


elcaro2k
08-28-2002, 06:32 PM
What is the best way to pass parameters to a new window? In other words I have a table on my current window and when I click on one row I would like to have a couple of the values avaliable in the new window?

Thanks for any help!!!

beetle
08-28-2002, 06:43 PM
Can you be more specific? Are you just linking to a new page, or opening a popup? Do you need help extracting the values to be sent as well (are they coming from TDs?) Or are they already in javascript variables?

This can be done for sure, I just need more info

elcaro2k
08-28-2002, 06:46 PM
it is in a new window and new page. Not sure of the defintion of popup.

I already have the vars in javascript.

beetle
08-28-2002, 07:09 PM
Popup = new window created with the window.open() method

Now, first you need to append your javascript variables to the URL in a standard query string format which is this:link.htm?var1=value1&var2=value2And so on, separating each variable/value pair with the &

Now depending on how you are opening this new window, you have several choices. Here's one example<script>
function doLink(nUrl) {
var urlStr = nUrl + "?var1=" + variable1 + "&var2=" + variable2;
window.open(urlStr,'_blank','');
}
</script>

<a href="javascript:doLink('page.htm');">Click here</a>Ok, this will open a new window with our query string properly constructed. This example assumes that variable1 and variable2 are global variables on this page.

Now, in our new window (page.htm) you will need my parseGetVars() (http://www.codingforums.com/showthread.php?s=&threadid=4555) function. This will extract the data from the query string into variables you can use on the page. And example for page.htm might look like this<html>
<head>
<title>Test</title>
<script>
function parseGetVars() {
var getVars = new Array();
var qString = unescape(top.location.search.substring(1));
var pairs = qString.split(/\&/);
for (var i in pairs) {
var nameVal = pairs[i].split(/\=/);
getVars[nameVal[0]] = nameVal[1];
}
return getVars;
}
var g = parseGetVars();
</script>
</head>
<body>

<script>
document.write(g['var1'] + "<br>");
document.write(g['var2']);
</script>

</body>
</html>

requestcode
08-28-2002, 07:21 PM
Another question would be if the window is already opened when you click on the row in the main window. If it is then you would go about it differently. If the window is already opened then you could populate a variable in the new window by using the new windows name and the variable name. (hopefully you opended the new window like this: window_name=window.open(...) ). If you opened the new window like this:
Newwin=window.open(...)

then you should be able to populate the variables this way from the parent window:
Newwin.variable_name="some value"

elcaro2k
08-29-2002, 12:21 PM
Thanks for the help guys, everything is working great now!