MoOx / pjax

Easily enable fast Ajax navigation on any website (using pushState + xhr)
MIT License
1.46k stars 125 forks source link

Override window.location ? #142

Closed nanook21 closed 6 years ago

nanook21 commented 6 years ago

Would it be possible to have pjax regex replace window.location calls into PJAX calls if they are on the same host?

I have some sites that I don't control the raw code, just the CMS and header/footer, and realized that it is using window.location in some weird places.

BehindTheMath commented 6 years ago

I played around with it for a while. Basically, you can't reassign window or window.location. So I don't think this will work.

nanook21 commented 6 years ago

Would it be possible to regex replace the content that comes back from the Ajax request before it gets processed?

BehindTheMath commented 6 years ago

If I'm not mistaken, assigning window.location doesn't make an AJAX request, it reloads the page completely.

nanook21 commented 6 years ago

I had meant the Ajax call that Pjax makes when loading in a new page. That comes back as a string of content doesn’t it?

So if all references to window.location in that string were replaced, window.location would never end up in the dom.

BehindTheMath commented 6 years ago

v0.2.6 will have a new pjax.handleResponse() method that you can use to do that.

If you want to try it now, you can install from Github. With NPM, that would be npm install --save moox/pjax@master.

Then you can do something like this:

pjax._handleResponse = pjax.handleResponse;

pjax.handleResponse = function(responseText, response, href) {
    if (responseText.contains("window.location") {
        // Fix the response here
    }

    pjax._handleResponse(responseText, response, href);
}
nanook21 commented 6 years ago

Great, thanks!