无 Javascript 检测脚本 + 重定向

IT技术 javascript redirect
2021-01-15 07:21:58

我想编写一个脚本来检测用户是否禁用了 javascript,如果是,则将它们重定向到另一个页面(比如 mysite.com/enablejavascript)

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

4个回答

Gdoron 已经提到了 noscript。元刷新¹一起,您可以重定向禁用 JavaScript 的用户。

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

<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+meta 刷新示例:http : //pastehtml.com/view/bsrxxl7cw.html

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

元刷新标签有一些缺点:

  • 如果页面重定向过快(少于2-3秒),使用下一页的“返回”按钮可能会导致某些浏览器返回重定向页面,然后再次发生重定向。这对可用性不利,因为这可能会导致读者“卡在”上一个网站上。
  • 读者可能希望也可能不想被重定向到不同的页面,这可能会导致用户不满或引起对安全性的担忧。
好的,这对我来说似乎很好用。但是 Visual Studio 声称 noscript 不能嵌入到 head 中,并且 meta 不能嵌入到 noscript 中。我的 DOCTYPE 是 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 并且 html 标签具有命名空间 <html xmlns=" w3.org/1999/xhtml "> 这些是否相关?
2021-03-14 07:21:58
+1。不知道那Meta refresh件事。好的。我在您的答案中添加了缺点。希望你不要介意
2021-03-24 07:21:58
HTML5 允许在文档中的任何位置使用元标记。除其他外,它们还用于 schema.org 上的微数据。HTML5 还允许在 head 中使用 noscript,只要它只包含 meta.js 和其他内容。
2021-03-25 07:21:58
请注意,对于可访问性,不推荐元刷新,这通常是有人可能没有打开 javascript 的原因。
2021-04-03 07:21:58

当 java-script 被禁用时,你想如何编写脚本...?

这是做不到的。当 javascript 被禁用时,您可以显示一条消息<noscript>

<noscript>MDN 上的标签

HTML NoScript 元素 () 定义了在页面上的脚本类型不受支持或浏览器中当前关闭脚本时要插入的 html 部分。

您应该在 noscript 元素中结合 HTML 重定向。我找到了这个JavaScript 重定向生成器。这是您的示例代码:

<!-- Pleace this snippet right after opening the head tag to make it work properly -->

<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://yourdomain.com/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://yourdomain.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://yourdomain.com/";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
    {
        document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->

试试这个:如果java脚本被禁用,那么显示一个php链接

<script type="text/javascript">

document.write("<button type='button' onclick='somefunction()' >Some Text</button>"); 

</script>

<noscript> 

<?php echo "<a href='redirectfile.php'>Some Text</a>"; ?> 

</noscript>
请更详细地描述您的解决方案。参考:如何回答
2021-03-28 07:21:58