How to wrap <noscript> tag to hide content when javascript disable

旧巷老猫 提交于 2019-11-29 02:32:44

问题


Let's say, I have HTML code like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
This is content.
</body>
</html>

And I want to add a <noscript> tag there. Which means, if the JavaScript disabled, it will show as a blank page.

And only when JavaScript is disabled, it will show "This is content text".

Please give me some examples to achieve. Thanks.


回答1:


An alternative to a JS approach as suggested by rahul is to display a div in the <noscript> element, covering the entire page:

<noscript>
    <div style="position: fixed; top: 0px; left: 0px; z-index: 3000; 
                height: 100%; width: 100%; background-color: #FFFFFF">
        <p style="margin-left: 10px">JavaScript is not enabled.</p>
    </div>
</noscript>



回答2:


Wrap all you contents inside a main div with display none and in the onload function change the display to block.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
</head>
<body>
  <div id="divMain" style="display: none">
    This is content.
  </div>
<noscript>
  JS not enabled
</noscript>
<script>
  document.getElementById("divMain").style.display = "block";
</script>
</body>
</html>



回答3:


It's a little odd.

You don't even need a 'noscript' for this. You can just have a blank first page, who's only content is a javascript of the form:

document.location = '/realpage.htm';

And then call that OnLoad, with jQuery, or whatever. This will mean if the client doesn't have scripting, they don't go anywhere, hence the page remains blank.

Edit:

Example, as requested:

<html>
<body onload="document.location = 'realpage.html';">
</body>
</html>



回答4:


The noscript tag works the other way around. To make the content visible when script is enabled, put it in an element that is hidden, and show it using script:

<div id="hasScript" style="display:none">
This is content
</div>
<script>document.getElementById('hasScript').style.display='';</script>



回答5:


This SHOULD really work! hide a content when javascript is not enabled Note: you can replace <noscript> with <noembed> or <noframes>, too.

<!-- Normal page content should be here: -->
Content
<!-- Normal page content should be here: -->
<noscript>
<div style="position: absolute; right: 0; bottom: 0;
     display: none; visibility: hidden">
</noscript>
<-- Content that should hide begin -->
**Don't Show! Content**
<-- Content that should hide begin -->
<noscript>
</div>
</noscript>
<!-- Normal page content should be here: -->
Content
<!-- Normal page content should be here: -->



回答6:


I had a very problem:

I wanted to show the full site when javascript was enabled. However, if javascript were disabled, not only did I want to show a "this site requires javascript..." message, but I still wanted to display the header & footer (which reside in header.inc and footer.inc, called by each content page) of my site (to look nice). In other words, I only wanted to replace the main content area of my site. Here's my html/css solution:

Header.inc file (location not important):

<noscript>
    <style>
        .hideNoJavascript {
            display: none;
        }  
        #noJavascriptWarning {
            display: block !important;
        }
    </style>
</noscript>

Header file (bottom):

<div id="noJavascriptWarning">The Ignite site requires Javascript to be enabled!</div>
<div class="container-fluid hideNoJavascript">
<!-- regular .php content here -->

CSS file:

#noJavascriptWarning {
    color: #ed1c24;
    font-size: 3em; 
    display: none;  /* this attribute will be overridden as necessary in header.inc */
    text-align: center;
    max-width: 70%;
    margin: 100px auto; 
}

In summary, my "javascript required" message is hidden per my default CSS rule. However, when the browser parses the tag, it overrides the default rule, resulting in main content being hidden and (everything except header/footer) and the javascript message being displayed. Works perfectly...for my needs! Hopefully someone else finds this useful :-)

Here are two screenshots with javascript enabled/disabled:




回答7:


Both the style tag and the meta refresh DO work inside noscript:

<noscript>

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

<meta http-equiv="refresh" content="0; url=blankpage.html">

</noscript>

The first line will display the current page but empty. The Second line will redirect to blankpage.html




回答8:


You could do something like

<body id="thebody">
  this is content.
  <script>
    document.getElementById('thebody').innerHTML = "<p>content when JS is enabled!";
  </script>
</body>


来源:https://stackoverflow.com/questions/1431137/how-to-wrap-noscript-tag-to-hide-content-when-javascript-disable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!