Reputation: 21
I have a ASP.net MVC project. i want to make a function in js that will execute only on the first load of this page in the current run of the program. In other words, if someone goes to a different page and comes back - i don't want it to happen again. but if i re-run the project from the beginning, i want it to happen.
i did my research - but everything i found - the ideas of Cookies and localstorage won't work for me, because they only work on the first load of the page of the user - so if we go out and rerun the project from the same computer, it won't work...
Anyone have an idea?
Upvotes: 0
Views: 1267
Reputation: 52270
I am not sure you have clearly defined "first" "come back" and "current run." These are not straightfoward concepts in a web application, which from a logical perspective never starts nor stops, nor is there any concept of order of access unless you have specifically written a means of maintaining workflow state, which is not necessarily trivial.
If you want a script that will only run one time regardless how many times the page is accessed from any machine, you can set an application variable after it runs. On subsequent requests for the page, check for the application variable, and omit the Javascript if the variable is present. Note that the script will run again if the application resets for any reason, e.g. app pool recycle, which may be automatic depending on your web server settings.
If you want a script that will only run one time per browser instance, set a cookie after it runs. On subsequent requests for the page, check for the cookie, and if it is present, skip the execution of the script. Note that if a user has more than one browser, he will be able to get the script to run more than once, because most browsers do not share a cookie jar.
If you want a script that will only run one time per browser session (e.g. if you close and open the browser, the script should run again), set a session cookie after it runs. On subsequent requests for the page, check for the cookie, and if it is present, skip the execution of the script.
If you want a script that will only run one time per user, you will need some means of identifying the current user. So that means you need an authentication mechanism. In that case, only run the script if the user has just authenticated-- set a flag in the user database indicating the user has witnessed the execution of the script, and check it before rendering the script again.
Upvotes: 1
Reputation: 494
You're partially there.
Or like the comments said, just use a session and expire it when the project restarts.
Upvotes: 0