如果 JavaScript 被禁用,如何重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2489376/
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-08-23 00:37:17  来源:igfitidea点击:

How to redirect if javaScript is disabled?

javascriptredirect

提问by zeckdude

I have a site which relies heavily on javaScript. I created a mirror site, which has all the JS as well as all the elements that require JS removed. What is a good, easy way to redirect users to the mirror site if they don't have javaScript enabled?

我有一个严重依赖 javaScript 的网站。我创建了一个镜像站点,其中包含所有 JS 以及所有需要删除 JS 的元素。如果用户没有启用 javaScript,有什么好的、简单的方法可以将用户重定向到镜像站点?

I tried this, but it doesn't seem very good:

我试过这个,但它似乎不是很好:

<noscript>
  <meta http-equiv="refresh" content="0; URL=nojs/index.php">
</noscript>

I also tried to putting header-redirect into the noscript tag, but that didn't work.

我还尝试将 header-redirect 放入 noscript 标记中,但这不起作用。

采纳答案by Matt

Make the no-JavaScript version of the site the default. Include a small script in there to redirect to the scripted site.

将站点的无 JavaScript 版本设为默认版本。在其中包含一个小脚本以重定向到脚本站点。

Or, abandon the use of a redirect entirely and go with Progressive Enhancement

或者,完全放弃使用重定向并使用渐进增强

回答by Tyler Carter

<noscript>
    <p>This site is best viewed with Javascript. If you are unable to turn on Javascript, please use this <a href="http://sitewithoutjavascript.com">site</a>.</p>
</noscript>

Some people purposely disable Javascript, and you might want to give them a chance to turn it on before redirecting them.

有些人故意禁用 Javascript,您可能希望让他们有机会在重定向之前打开它。

回答by Ben Gollow

Use this code that I came up with:

使用我想出的这段代码:

<noscript>
  <style>html{display:none;}</style>
  <meta http-equiv="refresh" content="0.0;url=nojs/index.php">
</noscript>

It uses style to block what's on the page so then people won't notice anything before it redirects. The only thing that annoys me is that I want something better than meta refresh as that can be blocked on some browsers like IE. A PHP header isn't really a solution as you can't put it in a noscripttag as it will just ignore it and write it out straight away.

它使用样式来阻止页面上的内容,因此人们在重定向之前不会注意到任何内容。唯一让我烦恼的是我想要比元刷新更好的东西,因为它可以在某些浏览器(如 IE)上被阻止。PHP 标头并不是真正的解决方案,因为您不能将其放入noscript标签中,因为它会忽略它并立即将其写出。

回答by Amy B

What is your definition of "not very good"?

你对“不太好”的定义是什么?

All my sites use:

我所有的网站都使用:

<noscript>
  <meta http-equiv="refresh" content="0; url=http://www.sadtrombone.com/" />
</noscript>

回答by Sasha

I wouldn't do client-side redirection, as that might seem annoying to the user. Instead, what I would do is use <noscript>to show the content of this JS-less site on the same page. It may be more work, but it would definitely be a smoother experience.

我不会做客户端重定向,因为这对用户来说可能很烦人。相反,我要做的是<noscript>在同一页面上显示这个无 JS 站点的内容。这可能需要更多的工作,但肯定会是一个更流畅的体验。

回答by Ben Gollow

I came up with a better solution than having to redirect the user as meta-refresh can be disabled in IE.

我想出了一个比重定向用户更好的解决方案,因为可以在 IE 中禁用元刷新。

Put this in the HEAD:

把它放在 HEAD 中:

<style>div#body{display:none;}</style>

<style>div#body{display:none;}</style>

Put this in the BODY:

把它放在身体里:

<noscript>NO JAVASCRIPT CONTENT HERE</noscript>

<noscript>NO JAVASCRIPT CONTENT HERE</noscript>

<noscript><div id="body"></noscript>JAVASCRIPT CONTENT HERE<noscript></div></noscript>

<noscript><div id="body"></noscript>JAVASCRIPT CONTENT HERE<noscript></div></noscript>

That way the tags are where they're meant to be.

这样标签就在它们应该出现的地方。