How to use PJAX? (PJAX With PHP?) How to use PJAX? (PJAX With PHP?) ajax ajax

How to use PJAX? (PJAX With PHP?)


By default, pjax expects new pages to be delivered without the chrome - a HTML snippet that will be used as the innerHTML of the container.

In your example the container would be the first <section> tag I suppose. I don't know if pjax guarantees that it will use the first element that matches a selector - it may just replace every matching element. Probably it would be better to use an ID selector, such as #main.

Anyway, it sounds like you weren't delivering HTML snippets, but just the whole page. This almost defeats the purpose of pjax, but it can be supported by specifying a fragment in the downloaded content. Almost always this will be a selector that matches the container which will be replaced.

So, assuming you use a container with @id=main you could call pjax with

$(function() { $("ul a").pjax("#main", { fragment: "#main" }); });

Make sure that pjax is called after the document loads, otherwise the container lookup will fail.

By the way, an easier way to switch to pushState assisted navigation is with my HTMLDecor project. It requires you to change your perspective on generating HTML pages, but once you've done that you just need to add the HTMLDecor.js script to your pages and pushState is used automatically when appropriate - no config needed.