Giter VIP home page Giter VIP logo

html5-history-anchor's Introduction

<a is="html5-history-anchor">

Extend the <a> tag with the HTML5 history API

Fully featured version of the pushstate-anchor

A link from 1992.

<a href="/home">Home</a>

Now using the HTML5 window.history API.

<a is="html5-history-anchor" href="/home" pushstate popstate
   title="Home Page" state='{"message":"New State!"}'>Home</a>

Clicking this link calls the HTML5 history API.

window.history.pushState({message:'New State!'}, 'Home Page', '/home');
window.dispatchEvent(new PopStateEvent('popstate', {
  bubbles: false,
  cancelable: false,
  state: {message:'New State!'}
}));

Install

Download or run bower install html5-history-anchor --save

Import

<link rel="import" href="/bower_components/html5-history-anchor/html5-history-anchor.html">
or
<script src="/bower_components/html5-history-anchor/html5-history-anchor.js"></script>

API

The API is a direct extension of the <a> tag and window.history. Examples:

Push a new state with history.pushState(null, null, '/new-state') and dispatch a popstate event.

<a is="html5-history-anchor" href="/new-state" pushstate popstate>New State</a>

Replace the current state with history.replaceState({message:'Replaced State!'}, null, '/replaced-state') and don't popstate.

<a is="html5-history-anchor" href="/replaced-state"
   replacestate state='{"message":"Replaced State!"}'>Replaced State</a>

Back button with history.back().

<a is="html5-history-anchor" back>Back</a>

Forward button with history.forward().

<a is="html5-history-anchor" forward>Forward</a>

Back 2 pages with history.go(-2).

<a is="html5-history-anchor" go="-2">Back 2 Pages</a>

Refresh the page with history.go(0).

<a is="html5-history-anchor" go>Refresh</a>

Notes

The HTML5 history spec is a bit quirky. history.pushState() doesn't dispatch a popstate event or load a new page by itself. It was only meant to push state into history. This is an "undo" feature for single page applications. This is why you have to manually dispatch a popstate event. Including both pushstate and popstate attributes on the link will push the new state into history then dispatch a popstate event which you can use to load a new page with a router.

  • history.pushState() and history.replaceState() don't dispatch popstate events.
  • history.back(), history.forward(), and the browser's back and foward buttons do dispatch popstate events.
  • history.go() and history.go(0) do a full page reload and don't dispatch popstate events.
  • history.go(-1) (back 1 page) and history.go(1) (forward 1 page) do dispatch popstate events.

Build, Test, and Debug Build Status

Source files are under the src folder. The build process writes to the root directory. The easiest way to debug is to include the source script rather than the minified HTML import.

<script src="/bower_components/html5-history-anchor/src/html5-history-anchor.js"></script>

To build:

html5-history-anchor's People

Contributors

erikringsmuth avatar rkusa avatar

Watchers

James Cloos avatar

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.