Googlebot移动版,此Javascript代码有什么问题?

时间:2019-12-12 13:32:20

标签: javascript mobile responsive googlebot

此代码(检测移动设备并重定向到优化的样式)

<script type="text/javascript">
        if (screen.width <= 720) {
        window.location = window.location.href + "?style=Mobile";
    }
</script>

在我所有的手机以及网站用户中都可以完美运行,但是当我在GoogleBot Mobile上对其进行测试时: https://search.google.com/test/mobile-friendly

我收到此警报消息:“哇!发生了错误, 请稍等,然后重试。”

看起来像该机器人无法访问url网站...有人知道为什么吗?

编辑:

我认为它可能与window.location.href有关,因为如果我使用特定的URL,则测试效果很好,但是我需要检测线程的当前URL。

1 个答案:

答案 0 :(得分:0)

您的代码将循环。

尝试一下

var srch = window.location.search;
if (srch.indexOf("=Mobile") == -1) window.location = window.location.href + "?style=Mobile";