有效的方法在head中添加noscript以包装重定向 [英] Valid way to add noscript in head for wrapping redirect

查看:203
本文介绍了有效的方法在head中添加noscript以包装重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < p>< p>所以我想一个简单的方法来处理被浏览器禁用的javascript。头> 
< title> JavaScript测试< /标题>
< noscript>
< meta http-equiv =刷新
content =1; url = nojs.html/>
< / noscript>
< / head>

nojs.html 有一些东西例如:

 < p>返回< a href =jstest.html>测试< / a>启用javascrpt后。< / p> 

在崩溃页面。



不是我的首选方法,但它很好,很简单,直到可以为没有javascript的用户制作出更优雅的东西。



但是,将< noscript> 元素放置在头部部分。当然,初步的测试工作,但我迷信,当我的代码是有效的,再加上我恨这实际上失败了现场测试。



<那么有没有一种有效的方式来做到这一点?也许把 noscript 包装在另一个元素中,比如对象标签?或者一些更简单的方法,我没有想到?

解决方案

我不确定为什么你需要重定向到另一个页面只是显示一条消息。我使用JS和一个小CSS来为我处理这些情况。像这样:

 < head> 
....
< script type =text / javascript> document.documentElement.className + =js< / script>

< link rel =stylesheettype ='text / css'href =css / layout.cssmedia =all/>
< / head>
< body>
< div id =noscript>请启用JavaScript,然后刷新此页面。本网站需要JavaScript< / div>
< div id =wrapper>
...
< / div>
< / body>

然后在 layout.css



  #wrapper {display:none} / *如果JS禁用,则隐藏* / 
.js #wrapper {display:block} / *显示是否启用了JS * /
.js #noscript {display:none} / *如果启用JS,则隐藏* /

通过这种方式,该类在呈现页面之前应用于 html 元素,因此您赢得了该页面当JS内容换出非JS内容时,不会产生闪烁。


So I was thinking a simple way to deal with javascript being disabled by the browser would be the following:

<head>
        <title>JavaScript Test</title>
        <noscript>
                <meta http-equiv="Refresh"
                        content="1;url=nojs.html" />
        </noscript>
</head>

And having the nojs.html have something like:

<p>Return to <a href="jstest.html">test</a> after enabling javascrpt.</p> 

At the crash page.

This isn't my preferred method, but it's nice and simple until something more graceful can be worked out for users without javascript.

However, it is not valid to put a <noscript> element in the head section. The preliminary tests worked anyway, of course, but I'm superstitious when it comes to my code being valid, plus I'd hate for this to actually fail a field test.

So is there a valid way to do this? Perhaps wrapping the noscript in another element, like an object tag? Or some even simpler way I'm not thinking of?

解决方案

I am not sure why you need to redirect to another page instead of just showing a message. I use JS and a little CSS to handle these situations for me. Something like this:

<head>
   ....
   <script type="text/javascript"> document.documentElement.className += " js"</script>

   <link rel="stylesheet" type='text/css' href="css/layout.css" media="all" />
</head>
<body>
    <div id="noscript">Please enable JavaScript, then refresh this page. JavaScript is required on this site</div>
    <div id="wrapper">
       ...
    </div>
</body>

Then in layout.css:

 #wrapper      { display: none  } /* Hide if JS disabled */
 .js #wrapper  { display: block } /* Show if JS enabled */
 .js #noscript { display: none  } /* Hide if JS enabled */

By doing it this way, the class is applied to the html element before the page is rendered so you won't get a flicker as the non-JS content is swapped out for the JS content.

这篇关于有效的方法在head中添加noscript以包装重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆