微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无Javascript检测脚本重定向

我想编写一个脚本来检测用户是否禁用了 JavaScript,如果是,将其重定向到另一个页面(假设mysite.com/enablejavascript)

但是,我不知道从哪里开始!谢谢你.

解决方法

Gdoron已经提到了noscript.与 meta refresh¹一起,如果禁用JavaScript,您可以重定向用户.

可以使用location.replace(URL)完成JavaScript重定向.

<head>
  <noscript>
    <Meta http-equiv="refresh" content="0; url=http://example.com/without-js" />
  </noscript>

  <script>
    location.replace('http://example.com/with-js');
  </script>
</head>

noscript元刷新示例:http://pastehtml.com/view/bsrxxl7cw.html

1)注意维基百科文章drawbacks部分!

Meta refresh tags have some drawbacks:

  • If a page redirects too quickly (less than 2-3 seconds),using the “Back” button on the next page may cause some browsers to move back to the redirecting page,whereupon the redirect will occur again. This is bad for usability,as this may cause a reader to be “stuck” on the last website.
  • A reader may or may not want to be redirected to a different page,which can lead to user dissatisfaction or raise concerns about security.

原文地址:https://www.jb51.cc/js/154245.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐