HTML5 History

15
HTML5 History API Copyright 2016. Jyaasa Technologies. All Right Reserved http:// jyaasa.com Sagun Shrestha Visionary, Jyaasa Technologies

Transcript of HTML5 History

Page 1: HTML5 History

HTML5 History API

Copyright 2016. Jyaasa Technologies. All Right Reserved

http://jyaasa.com

Sagun ShresthaVisionary,

Jyaasa Technologies

Page 2: HTML5 History

● What is history API?● Why history API?● How to use HTML5 history API?

Overview

Page 3: HTML5 History

What is history API?

Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com

Page 4: HTML5 History

● History object isn’t something new.● We can trace history object back to early 1990’s

browser as well● It was never a public standard until HTML5.● With history api we have more control over

browser's history.● We can add history and change the url of the

browser without refreshing page.● Supports most of the browsers except IE 8,9 and

opera mini 8.

Page 5: HTML5 History

Why history API?

Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com

Page 6: HTML5 History

● Browser create a history entry when a new page is loaded.

● Before history api only way to change URL was to load the whole page or to use hash values

● when using AJAX the page doesn't load but just a part of page gets loaded and browser doesn't track this.

● History API gives us ability it manipulate browser history to keep track of user's browsing pattern accurately.

● HTML5 provides two methods:○ history.pushState()○ history.replaceState()

● Both of these methods allow us to add or update the history of browser.

Page 7: HTML5 History

How to use history API?

Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com

Page 8: HTML5 History
Page 9: HTML5 History

● pushState and popState both take same number of arguments.○ “State” can store JSON string and will be

available for popState event.○ “Title” a title for the state○ “URL” a new history url entry.

■ This url won’t be reloaded■ If relative path is given the it’s resolved

to current URL■ It must be of same origin otherwise

pushState will throw exception.■ If it is not specified then it sets

documents current url

Page 10: HTML5 History

# Index.html

<html><head> <title>This is history api DEMO</title> <script type="text/javascript" src="./js/jquery-2.2.4.js"></script> <script type="text/javascript" src="./js/history_demo.js"></script></head><body> <a href="1" id="test1">number</a> <a href="new.html" class="text">new</a> <a href="old.html" class="text">old</a> <p class="sample_text">999</p> <div id="text_content"></div></body></script></html>

# new.html<p>This is NEW!</p>

#old.html<p>This is OLD!</p>

Page 11: HTML5 History

History_demo.js

// this is for Number$(document).ready(function(){ var count = 0; $('#test1').on('click', function(e){ e.preventDefault(); count = count + 1; data = count; url = "/"+count.toString() + ".html"; $(".sample_text").text(count); window.history.pushState({content: data}, null, url); });});

// This is for New && Old$(document).on("ready",function(){ $('.text').on('click', function(e){ e.preventDefault(); url = $(this).attr("href"); $.get(url).done(function(resp){ $("#text_content").html(resp); window.history.replaceState({content: resp}, null, url); }); });});

Page 12: HTML5 History

● Now we will add popState window eventlistener in history_demo.js.

window.addEventListener("popstate", function(e) { $(".sample_text").text(e.state.content); $("#text_content").html(e.state.content);});

● This event triggers when browser’s back is clicked:

Page 13: HTML5 History

● Using ruby to fire-up an HTTP server in projects home directory

~/home/project_dir: ruby -run -e httpd . -p 3000

● In browser, enter http://localhost:3000

Page 14: HTML5 History

DEMO

Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com

Page 15: HTML5 History

Thank you

Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com