Re: Getting script to run once the page fully loads

5,278 views
Skip to first unread message

arif

unread,
Jan 5, 2011, 1:19:34 AM1/5/11
to greasemon...@googlegroups.com
Bill Donnelly <donnelly <at> snowcrest.net> writes:

>
> Quote: "Greasemonkey only runs scripts when the page has loaded anyway"
>
> This isn't really, completely true. Or not true at all. (afaik)
>
> I think the person wants an "after onload" execution, which truly executes
> after the page has completely loaded.
>
> Check out one of my extensions here:
>
> http://www.snowcrest.net/donnelly/firefox/gmscripts/colorfix.user.js
>
> Specifically, the "unsafeWindow.addEventListener ("load", ...." part.
> Although I'm not sure if using "unsafeWindow.addEventListener" is the
> correct or best way to do it now.
>
> And you can get to the page's functions and variables. I would do it using
> the "unsafeWindow." thing, but there may be a better way to do it without
> using "unsafeWindow", whose use is discouraged as much as possible for
> security reasons.
>
> Did I get close or closer?
>


Very Close.
This is exactly what I want to do. But I cannot get the following code working!
Please help

function tester(){
window.location.href="http://www.google.com";
unsafeWindow.addEventListener ("load", function () { tester2() }, false);
}

The function tester2 never gets called

Anthony Lieuallen

unread,
Jan 5, 2011, 9:00:20 AM1/5/11
to greasemon...@googlegroups.com
On 01/05/11 01:19, arif wrote:
> function tester(){
> window.location.href="http://www.google.com";
> unsafeWindow.addEventListener ("load", function () { tester2() }, false);
> }

First: Don't use unsafeWindow here. "window.addEventListener" is
equivalent, and it is not unsafe.

> The function tester2 never gets called

Of course not. The immediately previous line changes the location to a
new document. So this page stops loading, and scripts in it stop running.

steveb

unread,
Jan 7, 2011, 11:25:43 AM1/7/11
to greasemonkey-users
I have the same problem with my script.
My script looks like the following snippet:

window.addEventListener("load", funX(){

window.location.href = "http://www.xxx.x";

// do something on www.xxx.x

},false);

// do something

window.addEventListener("load", funY(){

window.location.href = "http://www.yyy.y";

// do something on www.yyy.y
}, false);

what can i do to fix my code?











Anthony Lieuallen

unread,
Jan 7, 2011, 1:07:54 PM1/7/11
to greasemon...@googlegroups.com
On 01/07/11 11:25, steveb wrote:
> ...

> window.location.href ="http://www.yyy.y";
> // do something onwww.yyy.y
> }, false);
>
> what can i do to fix my code?

Changing the location always stops currently running scripts. You need
a new script to run at the new location.

Kwah

unread,
Jan 7, 2011, 1:27:53 PM1/7/11
to greasemon...@googlegroups.com

You should read what Anthony wrote and actually think about what is happening when your code runs. If you load a different page to the one you are currently on, then the script will stop running and everything starts over from a blank page.

What you are trying to do is slightly absurd and goes something like this:

Load new page
script starts running
Script adds event listener
Event listener gets called
Firefox executes the line that changes page
Rest of the script is ignored as a new page loads
Repeat above sequence of events

In other words, the "do something at page xxx/yyy" never gets to execute because you change the page (wipe the event listener) before firefox even gets to it.

On 7 Jan 2011 16:25, "steveb" <pchr...@gmail.com> wrote:


On 5 Jan., 15:00, Anthony Lieuallen <arant...@gmail.com> wrote:
> On 01/05/11 01:19, arif wrote:
>

...

I have the same problem with my script.
My script looks like the following snippet:

window.addEventListener("load", funX(){

 window.location.href = "http://www.xxx.x";

 // do something on www.xxx.x

},false);

// do something

window.addEventListener("load", funY(){

 window.location.href = "http://www.yyy.y";

 // do something on www.yyy.y
}, false);

what can i do to fix my code?












--
You received this message because you are subscribed to the Google Groups "greasemonk...

steveb

unread,
Jan 7, 2011, 2:17:40 PM1/7/11
to greasemonkey-users
thanks for the help. it works :)
Reply all
Reply to author
Forward
0 new messages