Vololodymyr
Vololodymyr

Reputation: 2288

How to determine if browser is supporting js pushState function?

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 ?

Upvotes: 5

Views: 2358

Answers (4)

bhavya_w
bhavya_w

Reputation: 10097

if(!!history && !!history.pushState){
   //browsers which support history and history's push state method
}

Upvotes: 0

raina77ow
raina77ow

Reputation: 106453

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.

Upvotes: 5

blue
blue

Reputation: 1949

try {
    window.history.pushState("string", "Title", "/new-url");
} catch ( e ) {
    window.location = "/new-url";
}

Upvotes: 5

Stefano Ortisi
Stefano Ortisi

Reputation: 5336

You simply check:

if (history.pushState) {

}

Upvotes: 7

Related Questions