简体   繁体   中英

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.

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.

我建议使用Modernizr

您可以使用Modernizr检查浏览器是否支持新的HTML5历史API( pushStatereplaceStatepopState ),或使用History.js获得完全跨浏览器兼容性。

I would suggest:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM