cookies - run javascript only once -
i need run javascript once, save cookies, then, when try execute function, check if cookies saved, if so, don't trigger function.
i'm sure script has error because doesn't need :
var prom = getcookie('prom'); if (prom === null || prom === '0') { if (document.getelementbyid('tst')){ var str=document.getelementbyid("tst").innerhtml; var n=str.replace("login","logout"); document.getelementbyid("tst").innerhtml=n; setcookie("prom",'1',1); } }
html code script triggers change 'login' in 'logout'
<a href="login" title="log in" class="login_button" id="tst">login</a></span>
cookies script :
function setcookie(c_name, value, exdays) { var exdate = new date(); if (exdays) exdate.setdate(exdate.getdate() + exdays); var c_value = escape(value) + ((exdays) ? '; expires=' + exdate.toutcstring() : ''); document.cookie = c_name + "=" + c_value; } function getcookie(name) { var re = new regexp(name + "=([^;,]+)"); var value = re.exec(document.cookie); return (value !== null) ? unescape(value[1]) : null; }
jsfiddle test if http://jsfiddle.net/8yzap/11/
change
if (prom = '0'){
to
if (prom === '0'){
(of course, assuming getcookie
, setcookie
persist similar sessionstorage).
Comments
Post a Comment