views:

79

answers:

4

I am trying to mimic this site's Javascipt required banner, and have the below divs which are being hidden if javascript is allowed/enabled, but I am getting a flash/glimps of it on page load.

<div id="Main_noJS">Craftystuff.com works best with JavaScript enabled</div>
<div id="PartOfMain_noJS"><br /></div>

CSS:

#Main_noJS {
    width: 100%;
    height: 23px;
    font-family: Arial;
    font-size: 111%;
    color: White;
    font-weight: bold;
    background: #AE0000;
    text-align: center;
    padding-top: 4px;
    position: fixed;
    z-index: 100;
}

JavaScript:

// hide the "Craftystuff.com works best with JavaScript enabled" banner, if JavaScript is working
if ($("#Main_noJS")) {
    $("#Main_noJS").hide();
    // hide the spacer between the main content and banner...
    $("#PartOfMain_noJS").hide();
}

So the banner is visible to start with, and only when javascript is enabled do I hide it

  • but javascript must take a second to get to work to hide things...

I would like to try to stop the glimps of the banner, when the page first loads, any help?

A: 

Hide it with CSS

display: none;
Bart van Heukelom
+5  A: 

Put the banner in a a <noscript> tag, documented here.

<noscript>
   <div>yada yada yada</div>
</noscript>
tvanfosson
+1  A: 

You can simply write out a tag that hides the element while the page loads, then it won't be visible waiting for the script to run:

<script>document.write('<div style="display:none;">');</script>
<div id="Main_noJS">Craftystuff.com works best with JavaScript enabled</div>
<script>document.write('</div>');</script>
Guffa
A: 

Thanks guys, tvanfosson's bit seems to have done the trick for me, any chance of getting an explanation to go with it though?

Am new to things and so sorry for replying here also.

Thanks again(for the link) tvanfosson...

carpenter
The old `<noscript>` tag says "ignore this stuff if Javascript is available". Thus, if Javascript is disabled, the contents of the tag are *not* ignored, so the `<div>` ends up in the document.
Pointy
Thanks Pointy, it sounds useful, can you use it more than once on a page? Just asking to save having to shutdown linux and give it a go but will try later anyway...
carpenter
@carpenter: this answer should have been a comment.
therefromhere
@carpenter - it's an HTML tag. You can use it as many times as you like, though I wouldn't go overboard. You're probably better off doing it once for the message then trying to get things to degrade gracefully in the event that javascript isn't available.
tvanfosson