views:

35

answers:

1

I'm trying to set up a bookmarklet that will go to a web page, enter in some login information, and then automatically click the Login button for me. My problem is that if I simply go to the page and then enter the login information sequentially, the script usually finishes executing before the form elements are loaded. So, I need some way to make the script wait until the page has loaded before entering my login information into the form. I've tried a few approaches so far, and none have worked.

For example:

javascript:window.location = "http://www.msn.com";
window.onload=function(){alert("Page Loaded");};

The alert dialog never shows up. I've also tried using a function that I call periodically using window.setTimeout, but that doesn't work either:

javascript:function doLogin() {
    if (document.getElementsByName("login").length > 0) {
        document.getElementsByName("login")[0].value = "myUserName";
    }
    else {
        window.setTimeout(doLogin,100);
    }
}
window.location="http://my.web.page";
doLogin();

Once again, the login information never gets entered. Does anyone know how I could do this?

+1  A: 

I think the problem is that once you change the location of the window (by the way, it should be window.location.href = 'http://www.msn.com'; since location is actually an object) the rest of your JavaScript gets wiped out.

You may need to place your JavaScript in a hidden frame (or iframe) and open the target site in a visible frame (or iframe). Then you can manipulate the content of the page from the hidden (i)frame.

Disclaimer: I haven't tested this so I'm not totally sure that it will work.

ntownsend