javascriptjquerycachingpjaxpopstate

How to make pjax:popstate work like a simple PJAX call


While using the popstate in pjax, I want the content to be loaded from the server again, instead of being replaced by cached content of the browser. If the cached content is being applied, the <script> inside the cached content is not at all working. So, is there any way to force a normal PJAX call to the server instead of using the cached data, when we simply hit the browser's Back/Forward buttons?

Please help. Thanks.


Solution

  • Carefully notice line 476 to 491 in the file jquery.pjax.js here in this link: jQuery PJAX by Chris Wanstrath (defunkt). The codes under the if actually calls the Cached Contents and the code under the else portion is what we only want.

      if (contents) {
        container.trigger('pjax:start', [null, options])
    
        pjax.state = state
        if (state.title) document.title = state.title
        var beforeReplaceEvent = $.Event('pjax:beforeReplace', {
          state: state,
          previousState: previousState                                                     
        })                                                                                 
        container.trigger(beforeReplaceEvent, [contents, options])                         
        container.html(contents)
    
        container.trigger('pjax:end', [null, options])
      } else {
          pjax(options)
      }
    

    So, now you know what we are going to do! Yes, just remove everything except this line:

        pjax(options)
    

    and you're good to go! It's strange that nobody gave me this solution. However, I've finally found a solution by myself, and I think this will help a lot of people.

    Thanks, anyways.