How to determine if browser supports js pushState function?

I want to use dynamic URL update through js script:

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

But if the browser is outdated and does not support this feature, it should just redirect to the new URL.

Is there an easy way to check this?

+4
source share
4 answers

The simplest (and most effective):

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

However, I would suggest using some installed history management solutions, like History.js .

+2
source

You just check:

if (history.pushState) {

}
+6
source
try {
    window.history.pushState("string", "Title", "/new-url");
} catch ( e ) {
    window.location = "/new-url";
}
+4
if(!!history && !!history.pushState){
   //browsers which support history and history push state method
}
0

Source: https://habr.com/ru/post/1534394/


All Articles