0

I'm trying to write a script (bookmarklet, really) which has two parts. At the end of the first part, I want to click a button on the page which takes me to a second page. I then want the script to continue running after the second page has loaded. Is this possible?

Cg2916
  • 1,117
  • 6
  • 23
  • 36
  • This is not possible, sorry. ;P –  Sep 02 '15 at 00:52
  • I don't think you can do that. But, you can emulate that. Why do you want the same script to run it? do you have some kind of state? Can you load the 'next' page in an iframe maybe – Cacho Santa Sep 02 '15 at 00:52

2 Answers2

0

Every web page load is treated separately by the browser, so there is no way to get a script to continue running where it started off. Here are a few solutions though:

1) Save state to cookies, then read the cookies from the script on the second page to pick up where you left off. For instance, you could save the user's name "John Doe" to a cookie in the first page, then the script in the second page could load the user's name from the cookie. This is probably what you'll want to end up doing.

2) Instead of loading a new page in the browser window, load your new page in an iFrame, and the script running in your outer window won't be interrupted. You can reach inside iFrames with JavaScript as long as they are on the same origin.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
0

You aren't able to load a new page and continue a script from the previous page. But you could ajax load the new page, use the history API to modify the URL, and your script would keep running.

Will
  • 19,661
  • 7
  • 47
  • 48