Is is possible to call a function after [removed] has loaded new URL?

后端 未结 4 925
一整个雨季
一整个雨季 2020-12-04 02:03

I\'d like to be able to call a jquery function once window.location has completed loading a URL. Is this possible? I can\'t seem to find anything online about this.

<
相关标签:
4条回答
  • 2020-12-04 02:30

    You can either use window.onload of the destination page (if you have access to modify the code of that page), or you can use window.onunload to have the alert be launched when unloading the current page. You cannot execute code on the current page after the new page has been loaded.

    0 讨论(0)
  • 2020-12-04 02:34

    Yes.

    This page demonstrates onload/onunload behavior.

    <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>
    
    0 讨论(0)
  • 2020-12-04 02:37

    Iframe

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

    I use this method to perform small automated scripts, that can't really on third-party plugins.


    Ajax

    Another method might be using ajax to load the page/body content. Then replacing your body with the newly loaded body and start executing the next functions.

    0 讨论(0)
  • 2020-12-04 02:44

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

    use window.location to redirect the user to your index, adding a query parameter (something like window.location=index.php?firstLogin=true ) and on your index redirect (using javascipt http 300, header() or whatever you are using) to the profile page after it ends loading if the parameter is set

    0 讨论(0)
提交回复
热议问题