OLD | NEW |
(Empty) | |
| 1 ## <a is="html5-history-anchor"> |
| 2 > Extend the `<a>` tag with the [HTML5 history API](http://www.w3.org/html/wg/dr
afts/html/master/browsers.html#the-history-interface) |
| 3 > |
| 4 > Fully featured version of the [pushstate-anchor](https://github.com/erikringsm
uth/pushstate-anchor) |
| 5 |
| 6 A link from 1992. |
| 7 ```html |
| 8 <a href="/home">Home</a> |
| 9 ``` |
| 10 |
| 11 Now using the HTML5 `window.history` API. |
| 12 ```html |
| 13 <a is="html5-history-anchor" href="/home" pushstate popstate |
| 14 title="Home Page" state='{"message":"New State!"}'>Home</a> |
| 15 ``` |
| 16 |
| 17 Clicking this link calls the HTML5 history API. |
| 18 ```js |
| 19 window.history.pushState({message:'New State!'}, 'Home Page', '/home'); |
| 20 window.dispatchEvent(new PopStateEvent('popstate', { |
| 21 bubbles: false, |
| 22 cancelable: false, |
| 23 state: {message:'New State!'} |
| 24 })); |
| 25 ``` |
| 26 |
| 27 ## Install |
| 28 [Download](https://github.com/erikringsmuth/html5-history-anchor/archive/master.
zip) or run `bower install html5-history-anchor --save` |
| 29 |
| 30 ## Import |
| 31 ```html |
| 32 <link rel="import" href="/bower_components/html5-history-anchor/html5-history-an
chor.html"> |
| 33 or |
| 34 <script src="/bower_components/html5-history-anchor/html5-history-anchor.js"></s
cript> |
| 35 ``` |
| 36 |
| 37 ## API |
| 38 The API is a direct extension of the `<a>` tag and `window.history`. Examples: |
| 39 |
| 40 Push a new state with `history.pushState(null, null, '/new-state')` and dispatch
a `popstate` event. |
| 41 ```html |
| 42 <a is="html5-history-anchor" href="/new-state" pushstate popstate>New State</a> |
| 43 ``` |
| 44 |
| 45 Replace the current state with `history.replaceState({message:'Replaced State!'}
, null, '/replaced-state')` and don't `popstate`. |
| 46 ```html |
| 47 <a is="html5-history-anchor" href="/replaced-state" |
| 48 replacestate state='{"message":"Replaced State!"}'>Replaced State</a> |
| 49 ``` |
| 50 |
| 51 Back button with `history.back()`. |
| 52 ```html |
| 53 <a is="html5-history-anchor" back>Back</a> |
| 54 ``` |
| 55 |
| 56 Forward button with `history.forward()`. |
| 57 ```html |
| 58 <a is="html5-history-anchor" forward>Forward</a> |
| 59 ``` |
| 60 |
| 61 Back 2 pages with `history.go(-2)`. |
| 62 ```html |
| 63 <a is="html5-history-anchor" go="-2">Back 2 Pages</a> |
| 64 ``` |
| 65 |
| 66 Refresh the page with `history.go(0)`. |
| 67 ```html |
| 68 <a is="html5-history-anchor" go>Refresh</a> |
| 69 ``` |
| 70 |
| 71 ## Notes |
| 72 The [HTML5 history spec](http://www.w3.org/html/wg/drafts/html/master/browsers.h
tml#the-history-interface) is a bit quirky. `history.pushState()` doesn't dispat
ch a `popstate` event or load a new page by itself. It was only meant to push st
ate into history. This is an "undo" feature for single page applications. This i
s why you have to manually dispatch a `popstate` event. Including both `pushstat
e` and `popstate` attributes on the link will push the new state into history th
en dispatch a `popstate` event which you can use to load a new page with a route
r. |
| 73 |
| 74 - `history.pushState()` and `history.replaceState()` don't dispatch `popstate` e
vents. |
| 75 - `history.back()`, `history.forward()`, and the browser's back and foward butto
ns do dispatch `popstate` events. |
| 76 - `history.go()` and `history.go(0)` do a full page reload and don't dispatch `p
opstate` events. |
| 77 - `history.go(-1)` (back 1 page) and `history.go(1)` (forward 1 page) do dispatc
h `popstate` events. |
| 78 |
| 79 ## Build, Test, and Debug [](https://travis-ci.org/erikringsmuth/html
5-history-anchor) |
| 80 Source files are under the `src` folder. The build process writes to the root di
rectory. The easiest way to debug is to include the source script rather than th
e minified HTML import. |
| 81 ```html |
| 82 <script src="/bower_components/html5-history-anchor/src/html5-history-anchor.js"
></script> |
| 83 ``` |
| 84 |
| 85 To build: |
| 86 - Run `bower install` and `npm install` to install dev dependencies |
| 87 - Lint, build, and minify code changes with `gulp` (watch with `gulp watch`) |
| 88 - Start a static content server to run tests (node `http-server` or `python -m S
impleHTTPServer`) |
| 89 - Run unit tests in the browser (PhantomJS doesn't support Web Components) [http
://localhost:8080/tests/SpecRunner.html](http://localhost:8080/tests/SpecRunner.
html) |
| 90 - Manually run functional tests in the browser [http://localhost:8080/tests/func
tional-test-site/](http://localhost:8080/tests/functional-test-site/) |
OLD | NEW |