3

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.

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 - Feature detection FTW. seems a bit overkill to use Modernizr just for this.
@James Many thanks, as a quick fix this has solved the problem.
Even though I suggested Modernizr, I agree!
6

I would suggest using Modernizr.

2 Comments

Thanks Ian, this looks good but may be a little over the top for where I am right now. Will bear in mind for the future though.
Ah, fair enough if you're only looking for one specific thing then I agree it's over the top.
1

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.

Comments

1

I would suggest:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.