javascript cookies, create and delete -


i'm working on page refreshes every 5 minutes

<meta http-equiv="refresh" content="1200;url=?meta_refresh=true" /> 

on page js script should run first 2 times page reloads. when page reload's third time, script should not execute.

so far, i've created cookie , given initial value of 0, every refresh increment it's value (rewrite cookie) , if value smaller 3 execute part of script. things if close tab , reopen page in tab, cookie has incremented value, , want start 0.

here's i've done far:

var value = 0;  $(document).ready(function() {      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 "";    }      function checkcookie() {      var cookievalue = getcookie('siterefreshcookie');      if (cookievalue !== '') {        var newvalue = parseint(getcookie('siterefreshcookie')) + 1;        if (newvalue < 3) {          //script executed          document.cookie = "siterefreshcookie="+ newvalue +";";        }      } else {                     document.cookie = "siterefreshcookie="+ value +";";      }    }    checkcookie();  })

could suggest using query string instead?

<meta http-equiv="refresh" content="1200;url=?meta_refresh=true&count=1" /> 

then asp programmer myself like:

<meta http-equiv="refresh" content="1200;url=?meta_refresh=true&count=<%=cint(0 & request.querystring("count")) + 1%>" /> 

but can achieve using php, or js imagine if have no back-end language suitable.

the problem using cookies tied website, rather window. if reset cookie unload function pete suggested, you'll run problems if example have 2 tabs open same page.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -