无JavaScript检测脚本+重定向

14
我想编写一个脚本,检测用户是否已禁用JavaScript,如果是,则将其重定向到另一个页面(比方说我的网站mysite.com/enablejavascript)。
但我不知道该从哪里开始!谢谢SO。

8
你需要使用 <noscript></noscript> - Brian Driscoll
1
@BrianDriscoll。为什么你没有写成答案呢?它有最多的赞! - gdoron
如果JavaScript被禁用,你如何确保脚本能够运行? - Anthony Rutledge
4个回答

26

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

1)请留意维基百科文章中的缺点部分!

Meta Refresh标签存在一些缺陷:

  • 如果页面重定向太快(少于2-3秒钟),在下一页上使用“返回”按钮可能会导致某些浏览器返回到正在重定向的页面,从而再次发生重定向。这对可用性不利,因为这可能会使读者“卡住”在上一个网站上。
  • 读者可能希望或不希望被重定向到另一个页面,这可能会导致用户不满意或引起安全方面的担忧。

3
+1. 不知道那个“Meta refresh”的事情。不错。我在你的回答中添加了缺点。希望你不介意。 - gdoron
好的,这对我来说似乎很有效。然而,Visual Studio声称noscript不能嵌入head,meta不能嵌入noscript。我的DOCTYPE是<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 并且html标签具有命名空间<html xmlns="http://www.w3.org/1999/xhtml"> 这两者之间有关系吗? - naskew
2
HTML5允许在文档中的任何位置使用元标签。它们被用于schema.org上的微数据等其他用途。HTML5还允许在head中使用noscript,只要它只包含元标签等其他内容。 - user239558
请注意,对于通常不开启JavaScript的可访问性而言,不建议使用meta refresh。 - G-Man

4
当JavaScript被禁用时,您如何编写脚本呢?这是不可能的。您可以使用<noscript>标签在JavaScript被禁用时显示一条消息。关于<noscript>标签的详细信息,请参阅MDN上的相关文档

<noscript>元素定义了一个html片段,如果页面上的某个脚本类型不受支持,或者浏览器当前已关闭脚本,则该片段将被插入。


2

你应该在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 -->

-3
尝试这个:如果 JavaScript 被禁用,则显示一个 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>

请更详细地说明您的解决方案。参考:如何回答问题? - askmish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接