GithubHelp home page GithubHelp logo

jquery.page.js's Introduction

page.js

Simple turbolink-like helper for jQuery.

Example

// Intiailize plugin

var page = new $p.page({
    container: '#content'
}).on('preload', function() {

    // Fires before ajax request

}).on('progress', function(e) {

    // Fires on progress

}).on('pushstate', function(data) {

    // Save data on state
    data.activeMenuElement = 'whatever';

}).on('load', function(e) {

    // Fire Google Analytics on page-load
    ga('set', { page: location.pathname.substr(1) + location.search, title: window.title });
    ga('send', 'pageview');

});

// Use page.js when clicking url

$(document).on('click.page.link', ' a:not([data-ajax="false"])', function (e) {
    // Ignore ctrl + click
    if (!e.metaKey && !e.ctrlKey) {
        var href = $(this).attr('href');

        if (href === (location.pathname + location.search)) {
            e.preventDefault();
            page.reload();
            return;
        }

        if (href !== null && href.indexOf('/') === 0) {
            e.preventDefault();
            page.go(href);
        }
    }
});

Events added on page.on() will fire each time the event is trigged. If you only want to fire an event once, you can use the  page.bind() instead.

In your master template add id="content" to the element where you want your ajax-requests to be displayed.

<div id="content">
    This content will be replaced by page.js when loading an url
</div>

Events

  • ready
  • preload
  • load
  • progress
  • error
  • pushstate
  • statechange

Ajax request

You can call page.js in the template of your ajax-request.

Example:

<!-- Your HTML -->

<script>
    // Set the page title
    page.setTitle('My loaded page');

    // Add styles to <head> if not already present
    page.loadStyles([
      '/css/specific-style.css'
    ]);

    // Add scripts to <head> if not already present
    page.loadScripts([
      '//code.jquery.com/jquery-3.1.1.slim.min.js'
    ]);

    // Fires when page and scripts is fully loaded
    page.ready(function() {

    });
</script>

Script or styles loaded through page.loadScripts or page.loadStyles will automaticially be added to the <head> of your page, if they aren't already present.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.