Javascript:使用更改的变量值重新加载页面
是否可以在不修改页面源的情况下将变量“a”更改为“true”,以便打印“foo”Javascript:使用更改的变量值重新加载页面,javascript,Javascript,是否可以在不修改页面源的情况下将变量“a”更改为“true”,以便打印“foo” var a=假; 如果(a)文件。填写(“foo”); 其他文件。填写(“bar”); 我可以从Chrome手动更改变量值。但是如何重新加载以查看“foo”,而不是“bar”您可以在大多数现代浏览器上使用localstorage,请参见 如果您计划支持较旧的浏览器,则可以设置cookie,然后在文档重新加载时读取它。 编辑:尝试此代码 //function to check support for local
var a=假;
如果(a)文件。填写(“foo”);
其他文件。填写(“bar”);
我可以从Chrome手动更改变量值。但是如何重新加载以查看“foo”,而不是“bar”您可以在大多数现代浏览器上使用localstorage,请参见 如果您计划支持较旧的浏览器,则可以设置cookie,然后在文档重新加载时读取它。 编辑:尝试此代码
//function to check support for localStorage
function checkLocalStorageSupport() {
try {
localStorage.setItem("x", "x");
localStorage.removeItem("x");
return true;
} catch (e) {
return false;
}
}
//function to read value of cookie
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
if (checkLocalStorageSupport()) {
if (localStorage.getItem('a') != null)
if (localStorage.getItem('a') == "true") //check that the value of localStorage is not null
document.write("foo"); //do operation if localStorage value is true
else
document.write("bar");
else
localStorage.setItem('a', 'false');
}
else {
if (getCookie('a') != null) //check that the value of cookie is not null
if (getCookie('a') == "true")
document.write("foo"); //do operation if cookie value is true
else
document.write("bar");
else
document.cookie = "a=false; expires=Thu, 31 Dec 2015 12:00:00 UTC"; //if no cookie exists set a cookie
}
//用于检查本地存储支持的函数
函数checkLocalStorageSupport(){
试一试{
setItem(“x”、“x”);
localStorage.removietem(“x”);
返回true;
}捕获(e){
返回false;
}
}
//函数读取cookie的值
函数getCookie(cname){
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
这应该如何工作?不能使用静态HTML页面和嵌入脚本在重新加载时保留状态。使用cookie或其他持久机制来存储状态。而不修改源?否。通过修改脚本,使其从localStorage
、sessionStorage
、cookies、AJAX读取状态值,还是使用服务器端语言提供的值?是的。这些只是指向潜在答案的链接;请发布一些相关的代码,因为链接会随着时间的推移而改变,使得这个答案变得无关紧要。
//function to check support for localStorage
function checkLocalStorageSupport() {
try {
localStorage.setItem("x", "x");
localStorage.removeItem("x");
return true;
} catch (e) {
return false;
}
}
//function to read value of cookie
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
if (checkLocalStorageSupport()) {
if (localStorage.getItem('a') != null)
if (localStorage.getItem('a') == "true") //check that the value of localStorage is not null
document.write("foo"); //do operation if localStorage value is true
else
document.write("bar");
else
localStorage.setItem('a', 'false');
}
else {
if (getCookie('a') != null) //check that the value of cookie is not null
if (getCookie('a') == "true")
document.write("foo"); //do operation if cookie value is true
else
document.write("bar");
else
document.cookie = "a=false; expires=Thu, 31 Dec 2015 12:00:00 UTC"; //if no cookie exists set a cookie
}