6

I would like a function to be executed only on the first page load and not when page is refreshed.

For example, it should not execute when the user clicks on the submit button, or on other page refresh.

I can avoid it in .net by using the ispostback property, but how can I do this in JavaScript?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rohit Bhardwaj
  • 269
  • 4
  • 20
  • 4
    I suppose you could use `sessionStorage` to keep track. – Ja͢ck Apr 15 '13 at 10:22
  • I would have suggested sessionStorage too its abit dirty I guess but it works –  Apr 15 '13 at 10:22
  • Maybe it's not important, but what would that function do? – Ja͢ck Apr 15 '13 at 10:23
  • 2
    I think you need to be more clear on your requirements. Should the function run only once, in like ever? or is there a period of time when it becomes acceptable that the function can be run again? You have to remember you cannot control what a user does. They might take a 24 hour break from it, or close the browser and re-open. Should these cause the function to run again? – musefan Apr 15 '13 at 10:25
  • i want that function should run only once when the page loads first time ..but it should not execute on other inner page refreshes.After it if user visit some other page and then it again comes to that url function then again that function should execute. – Rohit Bhardwaj Apr 15 '13 at 10:34
  • If I'm not completely wrong, ASP.NET's `IsPostBack` is set, if the user submits a form (e.g. by pressing a `submit` button). As far as I know the request method can't be checked client-side. Maybe you can set a JS-Flag on the server side. – Johannes Egger Apr 15 '13 at 10:35
  • @Jack..function will count the total no. views of that page.i will increase a count on each first time page load send the information to the database.but right now it is sending the information even on page refreshes. – Rohit Bhardwaj Apr 15 '13 at 10:36
  • @Jasd.. there is a ispostback property in .net.But i need the same in client side that is in javascript – Rohit Bhardwaj Apr 15 '13 at 10:39
  • are you trying to separate user initiated refresh (like, closing the page, clicking the browser's refresh button) from your program's refresh? if so, then you can use get variables, like in a form you will have a hidden input field `name="no_one_time_functions"`. – user2264587 Apr 15 '13 at 12:15
  • smells like bad design.. what exactly is you function doing ? – Gabriele Petrioli Dec 29 '13 at 13:13

2 Answers2

1

So you want to catch only the first page view. It is also known as unique page view. And then there are the page views, that are incremented at each page reload.

The first one should be tracked managing the session of the visit (for example using a cookie). The first time the page is visited, a cookie is set and the counter incremented. If the page is visited again if the cookie is already set the counter will not be incremented. Unless the cookies have been deleted and the user has changed the browser.

The second one will be incremented over and over again at each page refresh. Easly achived via javascript. To manage programmatically the page refresh status, I suggest you this answer. To manage dynamically the execution of a script, I suggest you the ClientScriptManager.

Community
  • 1
  • 1
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

Since you use asp and check for postback is ok for you, just render client-side flag;

in aspx:

<script type="text/javascript">window.ispostback = <%= Page.IsPostBack %>;</script>

in client script:

if (!window.ispostback) {
   // do stuff
};
Tommi
  • 3,199
  • 1
  • 24
  • 38