jquery-pjax icon indicating copy to clipboard operation
jquery-pjax copied to clipboard

Use pjax without making a server request

Open marczking opened this issue 10 years ago • 5 comments

Sometimes I want to only change some small things with JS only for which I don't need to make a request to the server. I would still like to change the history.pushState though.

When using history.pushState manually I get my wished behaviour, only problem is when navigating with back/forward-buttons pjax:popstate does not fire on the manually set pushstates.

So is there a way I can do this with pjax?

marczking avatar Apr 30 '14 11:04 marczking

+1 I am trying to do the same (I just need to fade an overlay to go back to the main main page and change url without breaking back/forward). Reloading the same main content just to change the url is overkill.

I already tried to simulate a pjax cachepush followed by a pushstate with a manually created state but pjax:popstate is not firing.

So is there a way to add a state that would trigger pjax:popstate manually ?

@marczking did you come up with a solution ?

trompx avatar Jun 25 '14 12:06 trompx

Right now it's tricky to mix pjax stuff with manual pushState/popstate stuff. You shouldn't expect pjax:popstate to fire, though, if you didn't use pjax. If it does, that's a bug.

I'll leave this open as a reminder that we need to improve pjax in scenarios where you mix-n-match it with other pushState functionality.

mislav avatar Jun 26 '14 01:06 mislav

This kind of requirement usually smells of an anti-pattern. You generally only want pushstate updates when big changes happen (such as general content replacement). Anything smaller is usually a code smell. You don't want URLs acting as application state management tools.

kirkbushell avatar Jan 06 '15 03:01 kirkbushell

    // disable and then re-enable to make sure our handler is before PJAX's
    $.pjax.disable();
    $(window).on('popstate', function (e) {
        if (!needPjax()) {
            $.pjax.disable();
            e.stopImmediatePropagation();
            // do your non-PJAX stuff here
            $.pjax.enable();
        }
    });
    $.pjax.enable();

samdark avatar Jan 22 '15 13:01 samdark

But overall I'd love to be able to prevent fragment replacing if pjax:popstate handler returns false or prevents default.

samdark avatar Jan 22 '15 13:01 samdark