简体   繁体   中英

How to tell when location.reload() has finished refreshing the page?

I am using location.reload() in a script and I want to run another script when the reload() function has finished.

Are there any events associated with reload() ?

All the scripts on the previous page are long gone by the time the page reloads. Therefore, there is no way to tell from the script that ran .reload() that the page has fully loaded.

Unless:

  • You work with an iframe, where you can reload just the iframe and listen for load events from outside the frame. That way, the script that listens to it is preserved.

  • Or, have the page contain an onload as suggested. However, the previous code, values and all are long gone as you just reloaded the page.

You might be solving the problem at the wrong angle as well. Instead of having the script, with all it's values, persist across page changes, why not make the data persist across page changes and read them upon every page? In this case, localStorage or cookies is your friend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM