Is it possible to call a function after window.location has loaded a new URL?

I would like to be able to call the jquery function as soon as window.location finishes loading the url. Is it possible? I can't seem to find anything on the Internet.

eg:

if(parseInt(msg.status)==1) { window.location=msg.txt; alert("This URL has finished loading") } 

Thanks, Paul

+6
source share
4 answers

You can use window.onload landing page (if you have access to change the code of this page), or you can use window.onunload to trigger an alert when the current page is unloaded. You cannot execute code on the current page after loading a new page.

+6
source

Yes.

This page demonstrates the behavior of onload / onunload.

 <html> <head> <script> window.doUnload = function(){ alert("Here!"); } window.doLoad = function(){ window.location="http://www.google.com"; } </script> </head> <body onload="doLoad();" onunload="doUnload();"></body> </html> 
+2
source

After the user logs in for the first time, I need to load my index page to initialize everything, but then I need to forward them to another page to complete the profile.

use window.location to redirect the user to your index by adding a query parameter (something like window.location=index.php?firstLogin=true ) and index redirection (using javascipt http 300, header () or whatever you use ) to the profile page after the download is completed, if the option is set

0
source

Iframe

One (ugly) method you could use is instead of using window.location, cleaning the body, adding an iframe with the appropriate path, and listening to its onload function. After that, you can run the code inside the iframe if it is not cross-site scripting.

I use this method to execute small automated scripts that cannot really be third-party plugins.


Ajax

Another method could be to use ajax to load the contents of the page / body. Then replace the body with the newly loaded body and begin the following functions.

0
source

Source: https://habr.com/ru/post/897725/


All Articles