pjax.js is a lightweight and efficient library for implementing Single Page Applications (SPA) in Laravel, CodeIgniter, or any PHP-based project. It enables partial page loads and seamless navigation without full-page reloads, enhancing user experience and performance.
- Partial Page Loading: Load only the required content via AJAX.
- Session Storage Caching: Cache pages for faster navigation.
- Dynamic Menu Updates: Automatically update active menu items based on the current URL.
- Customizable: Easily integrate with Laravel, CodeIgniter, or other PHP frameworks.
- Scroll Management: Control scroll behavior during navigation.
- Error Handling: Graceful handling of unauthorized or error responses.
npm install @softono/pjaxThen include the library in your project:
import "@softono/pjax";<script src="https://cdn.jsdelivr.net/npm/@softono/pjax/pjax.min.js"></script><script src="https://unpkg.com/@softono/pjax/pjax.min.js"></script>Clone or download the repository and include pjax.min.js in your project:
<script src="path/to/pjax.min.js"></script>pjax.js depends on jQuery, so ensure jQuery is loaded before pjax.min.js.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>Wrap your main content in a container with the ID main-container:
<div id="main-container" data-layout="main">
<div id="main-content" data-title="">
<!-- Main content goes here -->
<div></div>
</div>
</div>Add the pjax class to your links to enable PJAX navigation:
<a href="/example" class="pjax">Example Link</a>Initialize pjax.js when the DOM is ready. You can also define hooks to run custom code during navigation:
$(document).ready(function() {
// Hook executed when a PJAX link is clicked
pjax.onLinkClick = function(target) {
// e.g., Show a custom loader or update UI
$(target).addClass('active').siblings().removeClass('active');
};
// Hook executed after page content is updated
pjax.onPageLoaded = function(url) {
// e.g., Send pageview to analytics
console.log("Successfully loaded: " + url);
};
pjax.init();
});Ensure your server can handle partial requests by checking for the partial query parameter. For example, in PHP:
if (isset($_GET['partial']) && isset($_GET['layout']) && $_GET['layout'] == 'main') {
include 'partials/content.php';
}- Each link (
<a>tag) where you want to apply PJAX SPA functionality must include thepjaxclass. - The
data-pjax-cacheattribute is optional and should only be used for static pages that need to be cached in the browser.
To enable caching for specific links, add the data-pjax-cache attribute:
<a href="/example" class="pjax" data-pjax-cache="true">Cached Link</a>Control scroll behavior by setting the data-pjax-scroll attribute:
<a href="/example" class="pjax" data-pjax-scroll="false">No Scroll</a>Add the following script to the header of your HTML file:
<script>
var documentReadyFunctions = [];
function documentReady(fn) {
documentReadyFunctions.push(fn);
}
</script>To ensure JavaScript code runs after PJAX updates, include the following function call at the bottom of your view:
documentReady(function () {
//add your code here
});This function will execute all registered documentReady functions after the HTML content is updated. this is alternative to jquery $(document).ready()
pjax.onLinkClick(target)- Function executed when a PJAX-enabled link is clicked.
pjax.onPageLoaded(url)- Function executed after PJAX successfully loads and updates the page content.
pjax.init()- Initialize PJAX functionality.
pjax.loadPage(url, cache, scroll)- Load page content via AJAX.
pjax.updateContent(html)- Update the main container with new content.
pjax.updateActiveMenuByUrl()- Update active menu items based on the current URL.
pjax.routeLinks()- Set up click handlers for PJAX-enabled links.
Contributions are welcome! Feel free to submit issues or pull requests to improve this library.
This project is licensed under the MIT License. See the LICENSE file for details.
Special thanks to the open-source community for inspiration and support.