我想编写一个脚本来检测用户是否禁用了 javascript,如果是,则将它们重定向到另一个页面(比如 mysite.com/enablejavascript)
但是,我不知道从哪里开始!谢谢。
我想编写一个脚本来检测用户是否禁用了 javascript,如果是,则将它们重定向到另一个页面(比如 mysite.com/enablejavascript)
但是,我不知道从哪里开始!谢谢。
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秒),使用下一页的“返回”按钮可能会导致某些浏览器返回重定向页面,然后再次发生重定向。这对可用性不利,因为这可能会导致读者“卡在”上一个网站上。
- 读者可能希望也可能不想被重定向到不同的页面,这可能会导致用户不满或引起对安全性的担忧。
当 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>