无 Javascript 检测脚本 + 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9895012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:09:13  来源:igfitidea点击:

No-Javascript Detection Script + Redirect

javascriptredirect

提问by Hugo

I would like to write a script that detects whether the user has javascript disabled, and if yes, redirect them to another page (let's say mysite.com/enablejavascript)

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

However, I have no idea where to start! Thanks SO.

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

回答by kay - SE is evil

Gdoron mentioned noscript already. Together with meta refresh1 you can redirect users if they have JavaScript disabled.

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

A JavaScript redirect can be done with location.replace(URL).

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>

Example of noscript+meta refresh: http://pastehtml.com/view/bsrxxl7cw.html

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

1) Mind the drawbackssection of the Wikipedia article!

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

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.

元刷新标签有一些缺点:

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

回答by gdoron is supporting Monica

How do you want to write a script when java-script is disabled... ?

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

It's can not be done. You can show a message when javascript is disabled with <noscript>.

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

<noscript>tag on MDN:

<noscript>MDN 上的标签:

The HTML NoScript Element () defines a section of html to be inserted if a script type on the page is unsupported or if scripting is currently turned off in the browser.

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

回答by Patartics Milán

You should combine HTML redirect in a noscript element. I found this JavaScript redirectiongenerator. Here is your sample code:

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

回答by Zia

Try this: If java script is disabled then display a php link

试试这个:如果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>