Possible Duplicate:
Using Javascript to Open a New Page and Populate Form Values There
I'm trying to write a bookmarklet that will navigate to a certain web page, then execute a form fill script. The problem is that the script terminates as soon as the new page starts loading.
At first I thought I had to have the script run a loop to wait for the new page to load. I tried using a loop that tests for the new window.location, then I tried a loop that keeps trying to fill in the first element on the form until it verifies that the value is what the script attempted to insert. I also tried using the window.onload event handler to execute a function that fills the form. After some testing I determined that the bookmarklet simply stops running as soon as the page is loaded, no matter what I do.
Is there any way to prevent that from happening, or have the bookmarklet call some new code to execute once the page is loaded?
Here's what I tried:
javascript:
window.location = 'https://www.somesite.com/someform.pl';
while (window.location != 'https://www.somesite.com/someform.pl') {
}
// Commands to fill and submit the form
Also, I tried
javascript:
window.location = 'https://www.somesite.com/someform.pl';
while ((typeof document.getElementsByName('FirstElementName')[0] === "undefined") || (document.getElementsByName('firstelementname')[0].value != "myvalue")) {
document.getElementsByName('FirstElementName')[0].value='myvalue';
}
// Additional form fill commands, and command to submit
In both cases, all it does is take me to the web site. If I execute the same bookmarklet after the page has loaded, then it fills the form and submits it. I want both to be done with one invocation of the bookmarklet. If I set an alert in the loop, to give me a message in each iteration, the message box shows up, then disappears as soon as the new page loads. In fact, if you use window.location to navigate to a new page, then run an infinite loop that does nothing but repeatedly give you the same message, the loop terminates when the new page loads.
Note: I am aware that Greasemonkey can execute code every time a given page loads. I would like to find a way to do this with a bookmarklet, without the need for a browser extension. I use FireFox and will accept no substitute! :)