5

I want to use dynamic URL update through js script:

window.history.pushState("string", "Title", "/new-url");

But if browser is old and not supporing this function it should simply redirect to new URL.

Is there any simple way to check it ?

Vovan
  • 1,784
  • 5
  • 24
  • 40

4 Answers4

7

You simply check:

if (history.pushState) {

}
Stefano Ortisi
  • 5,180
  • 3
  • 30
  • 40
5
try {
    window.history.pushState("string", "Title", "/new-url");
} catch ( e ) {
    window.location = "/new-url";
}
blue
  • 1,954
  • 1
  • 10
  • 8
5

The easiest (and most performant):

if ('history' in window && 'pushState' in history) { // available

Still, I'd suggest using some established solutions for history management, like History.js.

raina77ow
  • 99,006
  • 14
  • 190
  • 222
0
if(!!history && !!history.pushState){
   //browsers which support history and history's push state method
}
bhavya_w
  • 7,962
  • 8
  • 26
  • 37