Detect first page load with jQuery?

后端 未结 4 1683
耶瑟儿~
耶瑟儿~ 2021-02-14 08:15

I need to detect the first time a page loads in jQuery so that I can perform some actions only when the page loads the first time a user navigates to that page. Similar to serve

4条回答
  •  春和景丽
    2021-02-14 08:47

    An event doesn't exist that fires only when the page is loaded for the first time.

    You should use jQuery's .ready() event, and then persist the fact that you've handled a first time page load using your method of choice (i.e. cookie, session variable, local storage, etc.).

    Note: This method will never be fool proof unless you can store this information at the user level in a DB. Otherwise, as soon as the user clears their cookies, or whatever method you choose, the "first time loaded" code will fire again.

提交回复
热议问题