Malasorte
Malasorte

Reputation: 1173

Do not run script if user has visited certain page

Any way to tell the script on page x.html not to run if user has visited page y.html? Would like to stay away from the cookies...

Can this be done with session or local storage? On stackoverflow I found only scripts with cookies...

EDIT: Can someone help with something like this:

if (there is data stored in localStorage for this page) {
// do stuff
}

Upvotes: 1

Views: 1890

Answers (1)

dersvenhesse
dersvenhesse

Reputation: 6414

Just read the localStorage and check if you already saved a visit. If not, run your script (like the alert in my example) and save the visit in localStorage.

var ls = localStorage.getItem('namespace.visited');
if (ls == null) {
    alert("Your first visit");
    localStorage.setItem('namespace.visited', 1)
}

http://jsfiddle.net/zgFgq/

Upvotes: 6

Related Questions