13

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???...

Pointy
  • 371,531
  • 55
  • 528
  • 584
Arun David
  • 2,576
  • 2
  • 16
  • 18

4 Answers4

29

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
}
James Gaunt
  • 14,162
  • 2
  • 36
  • 56
3

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

Marat Tanalin
  • 12,887
  • 1
  • 32
  • 50
0

You can use canisuse.js script to detect if your browsers supports history or not

caniuse.history()
Beka
  • 1,819
  • 5
  • 19
  • 26
-1

You can check weather or not a function is registered:

if (typeof history.pushState != 'undefined') {
    //Enabled
}

Then just replace //Enabled with whatever you wish to do if it's supported.

danniehansenweb
  • 463
  • 4
  • 14