David Gard
David Gard

Reputation: 12047

Check for HTML5 with JS

I have a page that contains some JS to update the browser history (pushState(), using HTML5). As IE8 does not support HTML5 users are being told that the page contains an error. While this doesn't deminish the functionality of the page, it doesn't look very professional, so I'm wondering if there is a check to see if the users browser supports HTML5 before running this code?

<script type="text/javascript">
/** Update history.state() (for back/forward links) */
var object = {
    ajax_string: '<?php echo make_ajax_string(); ?>',
    security: '<?php echo wp_create_nonce('updated-page-nonce'); ?>',
};
window.history.replaceState(object, '<?php echo $post->post_title; ?>', '<?php echo get_permalink($post->ID); ?>');
</script>

Thanks.

Upvotes: 3

Views: 1663

Answers (4)

vzwick
vzwick

Reputation: 11044

I would suggest:

if (!!(window.history && history.replaceState)) {
    // has replaceState support
}

Upvotes: 1

Simone
Simone

Reputation: 21272

You can use Modernizr to check if new HTML5 History APIs (pushState, replaceState, popState) are supported by the browser, or use History.js to have full cross-browser compatibility.

Upvotes: 1

James Allardice
James Allardice

Reputation: 165971

You can check if the replaceState method is available before running the code:

if(window.history.replaceState) {
    //Your code
}

If replaceState is undefined (which will be the case in browsers that do not support it) then the statement evaluates to false and the code is not executed.

Upvotes: 9

Ian Devlin
Ian Devlin

Reputation: 18870

I would suggest using Modernizr.

Upvotes: 6

Related Questions