Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check browser compatibility for HTML5 History API support?

Is there any way to check a browser whether it supports 'HTML5 History API' using JavaScript.

Do I have to check all the browsers and its versions with a long list of condition in if statement. Or simply like checking any object of function using 'if' statement is enough???...

like image 622
Arun David Avatar asked Feb 25 '12 17:02

Arun David


People also ask

Which browser does the HTML5 support?

HTML5 is now compatible with all popular browsers (Chrome, Firefox, Safari, IE9, and Opera) and with the introduction of DOCTYPE, it is even possible to have a few HTML features in older versions of Internet Explorer too.

What is HTML5 history API?

The HTML5 History API gives developers the ability to modify a website's URL without a full page refresh. This is particularly useful for loading portions of a page with JavaScript, such that the content is significantly different and warrants a new URL. Here's an example.

Which browser does not support HTML5 tags?

Mozilla Firefox browser version 2 doesn't support HTML5 semantic elements property. Firefox version 2 to 20 partially supports. Firefox version 21 to 63 supports HTML5 semantic elements property.

What is browser history API?

The DOM Window object provides access to the browser's session history (not to be confused for WebExtensions history) through the history object. It exposes useful methods and properties that let you navigate back and forth through the user's history, and manipulate the contents of the history stack.


2 Answers

Checking for the existence of a pushState() method on the global history object should be sufficient.

function supports_history_api() {
  return !!(window.history && history.pushState);
}

For more general HTML 5 feature detection I'd look at Modernizer

http://diveintohtml5.info/detect.html#modernizr

This will insulate your code from the messy specifics of each test, making the code more readable and less error prone. With the Modernizer script on your page you'd just do:

if (Modernizr.history) {
  // history management works!
} else {
  // no history support :(
  // fall back to a scripted solution like History.js
}
like image 153
James Gaunt Avatar answered Sep 19 '22 13:09

James Gaunt


Checking for history.pushState and history.replaceState objects existence should be sufficient, as it's generally sufficient with feature detection in general.

like image 28
Marat Tanalin Avatar answered Sep 21 '22 13:09

Marat Tanalin