# JavaScript - passing a variable from one window to another window?

**URL:** <https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715>\
**Category:** Factual Questions\
**Created:** [January 24, 2011, 12:26am UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715 "2011-01-24T00:26:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolf\_meister](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/wolf_meister/32/15202_2.png) [@wolf\_meister](https://boards.straightdope.com/u/wolf_meister)\
**Post date:** [January 24, 2011, 12:26am UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715/1 "2011-01-24T00:26:35Z")

</div>

I thought I knew JavaScript pretty well but maybe not.  
Basically, what I’m attempting is having one document (parent) open a second window (child) and have the variable values entered in the parent document to be transferred to the second.  
I looked at a great many JavaScript tutorials and couldn’t find a solution.  
Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![code\_grey](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@code\_grey](https://boards.straightdope.com/u/code_grey)\
**Post date:** [January 24, 2011, 2:37am UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715/2 "2011-01-24T02:37:04Z")

</div>

this article [Boutell.co.uk | Payday Loans Paid Out Instantly](http://www.boutell.com/newfaq/creating/scriptpass.html) suggests using window.name property if you are unable to do communication via a server-side script.

---

<div class="post-metadata">

**Author:** ![enipla](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@enipla](https://boards.straightdope.com/u/enipla)\
**Post date:** [January 24, 2011, 3:15pm UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715/3 "2011-01-24T15:15:13Z")

</div>

What I’ve done is pass the variables in the url like this -

window.open(“Print.aspx?ext=’” + extString + “’&PPI=’” + strPrintPPIs + “’”);  
And in the called window get the URL.  
var fullURL = window.location.href;

Then have javascript parse out the variable like this -

strPPI = fullURL.substring(fullURL.indexOf(’&’) + 6, fullURL.length - 2)

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [January 24, 2011, 3:49pm UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715/4 "2011-01-24T15:49:01Z")

</div>

> [@enipla](#):
>
> What I’ve done is pass the variables in the url like this -
> 
> window.open(“Print.aspx?ext='” + extString + “‘&PPI=’” + strPrintPPIs + “'”);  
> And in the called window get the URL.  
> var fullURL = window.location.href;
> 
> Then have javascript parse out the variable like this -
> 
> strPPI = fullURL.substring(fullURL.indexOf(‘&’) + 6, fullURL.length - 2)

A better way to do this is to pass the parameters on the anchor string, so you don’t end up polluting your server logs with client-side junk.

```auto

window.open( "foo.html#var1=" + var1 + ";var2=" + var2 );

```

Of course you can use whatever delimiters you want. Then in your new window

```auto

var params = window.location.hash;

```

And parse the stuff out of the params string however you like. jQuery has a number of good parameter parsers for query and anchor strings so you don’t have to reinvent the wheel.

---

<div class="post-metadata">

**Author:** ![wolf\_meister](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/wolf_meister/32/15202_2.png) [@wolf\_meister](https://boards.straightdope.com/u/wolf_meister)\
**Post date:** [January 27, 2011, 7:49am UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715/5 "2011-01-27T07:49:48Z")

</div>

Okay, let’s say I didn’t have too much success with those.

Maybe I should show something specific. (See next posting)

---

<div class="post-metadata">

**Author:** ![wolf\_meister](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/wolf_meister/32/15202_2.png) [@wolf\_meister](https://boards.straightdope.com/u/wolf_meister)\
**Post date:** [January 27, 2011, 8:34am UTC](https://boards.straightdope.com/t/javascript-passing-a-variable-from-one-window-to-another-window/568715/6 "2011-01-27T08:34:28Z")

</div>

Calculator 1 (passvar1) has three input boxes and when a number is input into all 3 boxes, clicking “Calculate Sum” puts the total of those 3 numbers into the output box.  
[http://www.1728.com/passvar1.htm](http://www.1728.com/passvar1.htm)

When “Calculate Product” is clicked, a new window is opened and it displays the numbers six, seven and eight with their product 336.  
Basically, how do I get the values of the 3 numbers (“num1” “num2” “num3”) into calculator2?

(The URL of the second calculator (the new window) doesn’t have to be shown but for those that want it: [http://www.1728.com/passvar2.htm](http://www.1728.com/passvar2.htm))  
(Thanks for the replies so far) 🙂
