Search code examples
smoothstate.js

Smoothstate : Get $(document) in onReady


How to get to $(document) in the onReady method ?

I've tried with smoothState.cache[smoothState.href] but couldn't make it works.

(fantastic plugin)

Thanks


Solution

  • I'm assuming you want to get at the document for the new page that's being loaded. Technically, $(document) exists in onReady, and it's the first document you visited on the site. After that, smoothState just dynamically updates it by swapping out content. So whatever existed on the previous page you were viewing is there as normal. Once the line $container.html( $newContent ); runs in your onReady method (assuming your code follows all the smoothState examples), you new content should be available.

    However, if you want to get at the actual, full document for the new page that got loaded up, not just what's contained in your wrapper div that gets swapped out, it's contained in smoothState.cache[smoothState.href].doc. It's got the header, the body, everything.

    A little reading of the smoothState source code shows that you can pull it into a useful format this way:

    var $newDoc = $("<html></html>").append( $(smoothState.cache[smoothState.href].doc) );
    

    At this point, you can run find queries or whatever you need to go look through things.