[英]Does anyone know how can I keep the Javascript variable even though the page has been refreshed?
Example: 例:
In the main page cliked on a button (NEW), the page then will using Javascript to open a new page in a new window by calling redirectPage(). 在按下按钮(NEW)的主页面中,页面将使用Javascript通过调用redirectPage()在新窗口中打开新页面。
In the main page clicked on a button (EXIT), then page then will call confirmExit(), then closeChildWindows() to closed all popup new window before redirect to another new page. 在主页面上单击一个按钮(EXIT),然后页面将调用confirmExit(),然后closeChildWindows()关闭所有弹出新窗口,然后重定向到另一个新页面。
However, the JS variable (childWindowHandles) will be always reset if I refresh the main page, and this cause the page unable to close all other popup window before relocated while EXIT button being clicked 但是,如果我刷新主页面,JS变量(childWindowHandles)将始终被重置,这会导致页面在重新定位之前无法关闭所有其他弹出窗口,同时单击EXIT按钮
Does anyone know how can I solve this problem? 有谁知道我怎么能解决这个问题? By able to keep the JS variable (childWindowHandles) even the main page being refresh?
能够保持JS变量(childWindowHandles)甚至主页面正在刷新吗?
var childWindowHandles = new Array();
function redirectPage(url)
{
childWindowHandles[childWindowHandles.length] = window.open(url)
}
function confirmExit(url)
{
closeChildWindows()
window.location=url
}
function closeChildWindows()
{
for (var loop=0; loop<childWindowHandles.length; loop++)
{
if (!childWindowHandles[loop].closed)
{
childWindowHandles[loop].close();
}
}
}
You can use cookies to persist values... 您可以使用cookie来保存值...
Edit: You might find useful a simple object that I use: 编辑:您可能会发现我使用的一个简单对象:
Usage: 用法:
// Store a key/value for 1 day:
cookieManager.set('name', 'a value', 1);
// Retrieve a value associated to a key:
var value = cookieManager.get('name');
// Remove a key/value:
cookieManager.remove('name');
Implementation: 执行:
var cookieManager = {
set: function (name, value, expireDays) {
var expireDate = new Date();
expireDate.setDate(expireDate.getDate() + expireDays);
document.cookie = name + "=" + escape(value) +
((!expireDays) ? "" : ";expires="+expireDate.toGMTString());
},
get: function (key) {
var start,end;
if (document.cookie.length > 0) {
start = document.cookie.indexOf(key + "=");
if (start != -1) {
start = start + key.length + 1;
end = document.cookie.indexOf(";",start);
if (end == -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(start,end));
}
}
return "";
},
remove: function (key) {
this.set(key, '', -1);
}
}
您可以使用cookie或window.name :) window.name来存储会话变量
Positives: 正面:
I also suggest you use javascript object for storing various values and serialize it using JSON and put that string into window.name. 我还建议您使用javascript对象存储各种值,并使用JSON将其序列化,并将该字符串放入window.name。
Just make sure you don't persist any vulnerable data inside... For security reasons. 只要确保你没有在内部保留任何易受攻击的数据......出于安全考虑。
You can use sessionStorage. 您可以使用sessionStorage。
Check this out: 看一下这个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.