0

我有这个javascript,我只需要运行一次而不是本地存储来保存状态。脚本在注销时更改登录。即使页面刷新,我也不希望执行脚本:

var str=document.getElementById("tst").innerHTML;
var n=str.replace("Login","Logout");
document.getElementById("tst").innerHTML=n;

html:

<a href="login" title="log in" class="login_button" id="tst">Login</a></span>

我尝试使用此代码,但现在根本没有触发脚本:

 var eventsFired = localStorage.getItem('fired');
 if (eventsFired != '1'){
 if (document.getElementById('tst')){
 var str=document.getElementById("tst").innerHTML;
 var n=str.replace("Login","Logout");
 document.getElementById("tst").innerHTML=n;
 localStorage.setItem('fired', '1'); 
 } } 

Js小提琴链接http://jsfiddle.net/9t4Wb/4/

我什至尝试使用 jquery.cookies.js 使用 cookie,结果相同:

var eventsFired = ($.cookie('eventsFired') != null)
? $.cookie('eventsFired')
: 0;

if (eventsFired == 0){

if (document.getElementById('tst'))
{
var str=document.getElementById("tst").innerHTML;
var n=str.replace("Login","Logout");
document.getElementById("tst").innerHTML=n;
eventsFired++;
$.cookie('eventsFired', eventsFired);
}}

Cookies Jsfiddle 测试 http://jsfiddle.net/JqxVj/5/

4

1 回答 1

0

If you need support for more than Chrome users, you can use AmplifyJS

It's let you use only one syntax on all browsers...

//Set value
amplify.store("myKey", myObject)

//Get value
amplify.store("myKey") // return myObject

You can also check the documentation and see the advantages of this plugin

于 2013-04-05T16:22:55.400 回答