0

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?

2
  • What about a session? You might also have a look at the html5 sessionStorage which works in most recent browsers and will "live" as long as the tab/browser is open Commented Jan 10, 2017 at 21:41
  • You could expire the page so that it is more difficult to get: stackoverflow.com/q/21456577/215552 Commented Jan 10, 2017 at 22:03

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You're partially there.

  1. Set a cookie A on the server response with latest project start time.
  2. If cookie B not already present, set cookie B on the client side with first load time and latest start time (from cookie A).
  3. On the client side, on each load, check if cookie A == cookie b's latest start time. If not equal, run your function and update cookie B with latest start time.

Or like the comments said, just use a session and expire it when the project restarts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.