javascript - Changing link URL after every 5 visits -
what i'm trying accomplish following.
i have link on website, want change link after every 5 visits or "page refreshes" user , have loop.
so example visit site , download button links site called "www.site1.com". refresh site 5 times , download button link changes "www.site2.com". if refresh 6th time goes original.
i have not been able find searching through forums shows i'm trying accomplish here. experimenting window.onload , setinterval function changes link every 5 seconds. anyway transition every 5 seconds every 5 page visits?
window.onload = function() { function changeurl(){ document.getelementbyid("link").href = "www.site1.com"; } setinterval(changeurl, 5000); }
you want use javascript localstorage
or sessionstorage
this. below example of code using localstorage
example
window.onload = function() { if (localstorage.visits) { //if value in local storage increase it's value localstorage.visits = number(localstorage.visits) + 1; } else { //if value isn't in local storage set 0 localstorage.visits = 0; } //check if number of visits greater 5 , set link accordingly if(localstorage.visits > 5) { document.getelementbyid("link").href = "www.site2.com"; } else { document.getelementbyid("link").href = "www.site1.com"; } }
this work if browser supports localstorage
. 1 thing note well, value not reset if close browser page. if want reset when page closed, that's sessionstorage
for.
one more thing note: user can clear localstorage
clearing browser data. if wanted more persistent don't believe there's javascript solution (feel free correct me if i'm wrong this!)
Comments
Post a Comment