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.
source share