本文介绍了用JS检查HTML5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面包含一些JS来更新浏览器历史记录( pushState(),使用HTML5)。由于IE8不支持HTML5,因此用户被告知该页面包含错误。虽然这不会破坏页面的功能,但它看起来不太专业,所以我想知道在运行此代码之前是否检查用户浏览器是否支持HTML5?

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>

谢谢。

推荐答案

在运行代码之前,您可以检查 replaceState 方法是否可用:

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

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

如果 replaceState undefined (在不支持的浏览器中会出现这种情况)然后语句评估为 false ,代码不会被执行。

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.

这篇关于用JS检查HTML5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:34