0

On facebook.com, where clicking on a 'link' appears to load a new page but (I believe) really just updates the existing page with new code, how can I know when that new content is fully loaded? For example, when you go to facebook.com, you start at your 'home' page (the news feed). When you click on your name in the navbar you're taken to facebook.com/yourname. However, it's not really a new page as a userscript doesn't get reloaded.

Because of this I wrote a little checker for my userscript that watches to see if the current href of the page has changed:

var startURL;
var curURL;

function main() {
    startURL = window.location.href;
    console.log("initial page. src: "+startURL);
    setInterval(checkfornewpage, 500);
}

function checkfornewpage() {
    curURL = window.location.href;

    if(curURL != startURL) {
        console.log("new page. src: "+curURL);
        // do something
        startURL = curURL;
    }
}

main();

This works fine and notifies me when the page has changed. But when trying to then access elements within that new page I'm finding that what I get back is from the old page. I presume this is because even though the window.location.href has changed, the rest of the page isn't fully loaded.

So then I decided to look for some content on the new page that I know will change to clue me into whether the new content is fully loaded. I chose the body tag classes because the list of classes for body is always different with each new page. I wrote a function to watch for this, but even after the body class list has fully changed, my queries are still bringing up old page data.

I also tried calling load() on the body element, but that never fires.

I don't want to use some kind of generic setTimeout that just waits long enough...I want to know precisely when it's loaded so I can move forward immediately. Ideas?

mix
  • 6,943
  • 15
  • 61
  • 90
  • This is for Chrome userscripts, right? (I have FF Greasemonkey code for this but haven't ported it to GrabAll's browser yet.) – Brock Adams Jun 15 '12 at 22:44
  • i want it to work on both, but yes, i'm aimed at chrome right now – mix Jun 15 '12 at 23:13
  • brock, any hints on how you did it in FF would be appreciated. i keep trying all kinds of various checks on the new content but nothing is reliable so far. – mix Jun 15 '12 at 23:59
  • 1
    Use the `the waitForKeyElements()` utility similar to [this answer](http://stackoverflow.com/a/10664942/331508) (and others). Optionally, trigger off of the `hashchange` event -- but this is probably not necessary. ... ... Finally, your last three questions seem like duplicates. You might consider deleting one or two and editing the survivors, lest a moderator smite ye. – Brock Adams Jun 16 '12 at 00:11
  • thx for the leads. and point taken on the dupes...didn't mean to keep rewriting the same question...they seemed different and more developed in the moment than they do now. i deleted one and requested deletion of the other, as this one seems more on point than those two. – mix Jun 16 '12 at 01:28

0 Answers0