Override window.location ? #142

Closed
opened 2018-03-16 16:39:03 -05:00 by nanook21 · 6 comments
nanook21 commented 2018-03-16 16:39:03 -05:00 (Migrated from github.com)

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.

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 2018-03-17 23:10:41 -05:00 (Migrated from github.com)

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.

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 2018-03-18 12:42:34 -05:00 (Migrated from github.com)

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

Would it be possible to regex replace the content that comes back from the Ajax request before it gets processed?
BehindTheMath commented 2018-03-18 13:04:48 -05:00 (Migrated from github.com)

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

If I'm not mistaken, assigning `window.location` doesn't make an AJAX request, it reloads the page completely.
nanook21 commented 2018-03-18 13:56:26 -05:00 (Migrated from github.com)

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.

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 2018-03-18 14:24:05 -05:00 (Migrated from github.com)

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);
}
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 2018-03-18 14:30:20 -05:00 (Migrated from github.com)

Great, thanks!

Great, thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: iLoveElysia/pjax#142