Results 1 to 4 of 4

Thread: Extremely basic Javascript snippet, who can be the first to remember the details?

  1. #1
    Good.Karma's Avatar
    Good.Karma is offline Unknown Net Builder
    Join Date
    Mar 2010
    Posts
    27
    Thanks
    4
    Thanked 1 Time in 1 Post

    Post Extremely basic Javascript snippet, who can be the first to remember the details?

    Hi all,
    This post is about very simple code and it is also something almost everybody will eventually need. I lost track of a really basic javascript snippet and now it's time to find it again:

    HTML Code:
    if javascript is off
    {show this div}
    
    if javascript is on
    {show this div}
    I'll also google it, and try to be the first to answer.
    Thanks

  2. #2
    Kovich's Avatar
    Kovich is offline Community Guardian
    Join Date
    Jan 2009
    Location
    Philadelphia, Pennsylvania
    Posts
    1,797
    Blog Entries
    30
    Thanks
    453
    Thanked 420 Times in 279 Posts
    If javascript is turned off, you would use:

    <noscript>
    Whatever you put here will display if JS is off.
    </noscript>

    And if javascript is on, then whatever javascript you have will be displayed normally.

  3. #3
    Good.Karma's Avatar
    Good.Karma is offline Unknown Net Builder
    Join Date
    Mar 2010
    Posts
    27
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Kovich View Post
    If javascript is turned off, you would use:

    <noscript>
    Whatever you put here will display if JS is off.
    </noscript>

    And if javascript is on, then whatever javascript you have will be displayed normally.
    Thanks Kovich,
    Your answer is correct, but I was trying to re-discover something I'd seen once before:

    • <noscript /> javascript tag doesn't work in all old browsers
    • <div id="noscript"> is another option, and it might work in all the oldest browsers? (Did the oldest browsers even allow 'div' tags?)


    I finally re-discovered...
    Try something like this. It degrades gracefully and doesn't require a <noscript /> tag. (source = 1st answer @link)

    Code:
    <div id="hasJavaScript" style="display: none">
        <!-- Contents hidden when JavaScript is disabled -->
    </div>
    
    <div id="noscript">
        <!-- Contents shown only when JavaScript is disabled -->
    </div>
    
    <script type="text/javascript">
    
        document.getElementById('hasJavaScript').display = 'block';
        document.getElementById('noscript').display = 'none';
    
    </script>
    IMO, the above code is simple, & a really clean way to deal with javascript issues.

  4. #4
    TopDogger's Avatar
    TopDogger is offline Über Hund
    Join Date
    Jan 2009
    Location
    Hellfire, AZ
    Posts
    2,946
    Thanks
    341
    Thanked 883 Times in 671 Posts
    Quote Originally Posted by Good.Karma View Post

    <noscript />
    javascript tag doesn't work in all old browsers
    That is not a valid closing tag for the noscript tag. The proper closing tag is </noscript>. If you use the proper closing tag is should work in all older browsers. The noscript tag has been around since the 1990s, but there is no such thing as a <noscript /> tag.

    The noscript tag is a container tag. In other words, you have to have something between an opening and closing tag in order for it to work. The trailing slash is only used for stand-alone tags that do not have a closing tag, such as br, img, meta, hr, etc.

    The JavaScript trick to determine if JavaScript is enabled is slick, but it might be easier to use the <noscript> tag.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


Similar Threads

  1. Rich Snippet Testing Tool
    By tmongy in forum Building
    Replies: 0
    Last Post: 21 September, 2010, 00:33 AM
  2. [WTS] An Extremely High Quality Site on TECHNOLOGY - PR 6
    By Divyesh in forum Links
    Replies: 0
    Last Post: 8 March, 2010, 18:09 PM
  3. IP Details: PHP Class with Example By Me
    By chetan in forum Programming
    Replies: 5
    Last Post: 26 September, 2009, 16:08 PM
  4. Can you identiy what the snippet does
    By Mr.Bill in forum Programming
    Replies: 4
    Last Post: 20 January, 2009, 16:10 PM
  5. Extremely Basic Unix Run Down
    By Charles in forum Managing
    Replies: 2
    Last Post: 18 December, 2008, 15:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •