0

I am beginner to JavaScript. I am opening a html page, whose JavaScript file has a global variable token.

In the JavaScript file, I am opening another html link on some condition using the code:

if(data.status==="Success"){
  window.open("http://172.19.101.65:8001/","_self")
}

Now in the JavaScript file of the above html link, whichever is loaded, I am not able to access the value token through window.token.

I am not sure whether my approach is right or wrong. I would be thankful for the help.

Albzi
  • 15,431
  • 6
  • 46
  • 63
siddiq rehman
  • 145
  • 1
  • 2
  • 18
  • 1
    The `window` is only the current window/tab, if you want to share data between them then you can add them as query params in the URL or store them in some client-side storage (cookies/localstorage). Only noticed you're using `_self` this will open that link as the current page, effectively clearing any global JS variables you have set. – George Apr 20 '17 at 10:30
  • Thanks for reply. I cannot pass the token as query params in URL. How do I do it in client storage? – siddiq rehman Apr 20 '17 at 10:34
  • Have a look [here](https://www.w3schools.com/html/html5_webstorage.asp) (or look at a couple of the answers below), it should point you in the right direction if you want to use localstorage. Be aware it's [not supported by some older browsers](http://caniuse.com/#search=localstorage) – George Apr 20 '17 at 10:37

4 Answers4

0

You should be able to access parent window variables via following code in popup target if parent window is open

parent.window.token

Since You are loading the new window with target as "_self" your previous window gets closed and the parent.window data is inaccessible.

I would recommend passing the token via localstorage, cookies or querystrings in your case. Or open the new window on blank target and then close its parent after accessing token of parent.

Bhavesh B
  • 1,121
  • 12
  • 25
0

you can try following code

var opener = window.opener;
if(opener) {
    var oDom = opener.document;
    var elem = oDom.getElementById("your element");
    if (elem) {
        var val = elem.value;
    }
}
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26
0

you cannot access the token variable because window.open makes a redirect thus your execution context is lost togheter with all the variable appended to the global object (in your case window.token)

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

What you can do is pass the token as part of the url or use the local storage

window.localStorage.setItem('token', window.token);

and then fetch it as soon as the page is loaded

var token = window.localStorage.getItem('token');

https://developer.mozilla.org/it/docs/Web/API/Window/localStorage

Karim
  • 8,454
  • 3
  • 25
  • 33
  • Its not working for me, i am getting the value as null :( – siddiq rehman Apr 20 '17 at 11:04
  • check inside chrome developer tools, in the Application tab, under Local storage if you find the key 'token'. if not you made an error when you called the setItem(). remember to set the token before calling the window.open – Karim Apr 20 '17 at 11:18
  • will the local storage will be avaiable if the second URL is also having the same IP and port as first URL ?..thanks – siddiq rehman Apr 21 '17 at 05:48
  • the localstorage is a memory inside the browser, associated to a specific domain, so yes it will be available. – Karim Apr 21 '17 at 07:47
0

You can use local storage: Work across pages

Set item: localStorage.setItem('token','value')

Get Item : localStorage.getItem('token')

PS: You can also use a cookie. Suitable and supported on almost every browser ;)

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47