javascript中的全局变量无法正常工作

时间:2022-06-30 16:49:43

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

我是JavaScript的初学者。我正在打开一个html页面,其JavaScript文件有一个全局变量令牌。

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

在JavaScript文件中,我在某些条件下使用代码打开另一个html链接:

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.

现在在上面的html链接的JavaScript文件中,无论加载哪个,我都无法通过window.token访问值标记。

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

我不确定我的方法是对还是错。我会感谢你的帮助。

4 个解决方案

#1


0  

You can use local storage: Work across pages

您可以使用本地存储:跨页面工作

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

设置项:localStorage.setItem('token','value')

Get Item : localStorage.getItem('token')

获取物品:localStorage.getItem('token')

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

PS:你也可以使用cookie。几乎每个浏览器都适用和支持;)

#2


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;
    }
}

#3


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)

你无法访问令牌变量,因为window.open进行了重定向,因此执行上下文丢失了,所有变量都附加到全局对象(在你的情况下是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

您可以做的是将令牌作为URL的一部分传递或使用本地存储

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

#4


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.

由于您正在加载目标为“_self”的新窗口,因此您的上一个窗口将关闭,而parent.window数据将无法访问。

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.

我建议您通过localstorage,cookies或querystrings传递令牌。或者在空白目标上打开新窗口,然后在访问父级令牌后关闭其父级。

#1


0  

You can use local storage: Work across pages

您可以使用本地存储:跨页面工作

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

设置项:localStorage.setItem('token','value')

Get Item : localStorage.getItem('token')

获取物品:localStorage.getItem('token')

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

PS:你也可以使用cookie。几乎每个浏览器都适用和支持;)

#2


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;
    }
}

#3


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)

你无法访问令牌变量,因为window.open进行了重定向,因此执行上下文丢失了,所有变量都附加到全局对象(在你的情况下是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

您可以做的是将令牌作为URL的一部分传递或使用本地存储

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

#4


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.

由于您正在加载目标为“_self”的新窗口,因此您的上一个窗口将关闭,而parent.window数据将无法访问。

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.

我建议您通过localstorage,cookies或querystrings传递令牌。或者在空白目标上打开新窗口,然后在访问父级令牌后关闭其父级。