46

I want to change URL without without reloading the page. The possible solution I found is

window.history.pushState('page2', 'Title', '/page2.php');

but some browser like Firefox 3.5, IE6+ does not support this, so for them solution is

var uri = window.location.href;

but the issue is how to discover if a browser supports history.pushstate or not?

Is TRY CATCH is the possible solution or any thing else.

Ashish Agarwal
  • 6,215
  • 12
  • 58
  • 91

1 Answers1

87
if (history.pushState) {
  // supported.
}

Quickest test is to run this in the browser console to see if it's supported:

if (history.pushState) { alert('supported'); }

Also notice that in FF typeof(history.pushState) returns "function", while in IE it returns "undefined"

Kon
  • 27,113
  • 11
  • 60
  • 86
  • 2
    With Firefox, when there is no actually history in the push stack (when you are the root of the domain), history.pushState return actually "undefined". I don't know if this is a bug, but it's really annoying. – Gael.D Mar 21 '13 at 09:37
  • Amendment to this answer. Use if(typeof window.history.pushState != 'undefined') to avoid error message on some old mobiles. – stonyau Nov 21 '18 at 10:18