www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: detect javascript enabled or disabled

  1. #16
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by vagner View Post
    Create a default.asp (in my case I'm using Classic ASP) and insert only this lines:

    <script type="text/javascript" language="javascript1.5" >
    location.href='/home.asp';
    </script>
    <meta http-equiv="Refresh" content="1;URL=/home.asp">
    that's the oppisite of the request: we want to send non-scripters packing, not punish 95% of our audience with an extra redirect...

  2. #17
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787

    Eureka

    EUREKA!

    i came up with the following concept, based on the notion of using javascript to "cancel" the meta.
    the prior posted attempts are either invalid, don't work, or send JS users away.

    the following code accomplishes the original task without the other codes' limitations:

    Code:
    <!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>	<title>js version</title>
    
    	<script>document.write("<"+"script type='hide'>")</script>
    		<meta http-equiv="Refresh" content="1;URL=/home.asp">
    	<script>document.write("<\/script>")</script>
    
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
       <body>  hello world  </body>
    </html>
    it works by writing an opening script tag before the meta, then closes off that script after the meta with another doc.write call.

    If javascript is enabled, the meta becomes just text content in an unknown-type script tag, which is ignored.

    if javascript is disabled, the script tag wrapper never gets applied, so the meta sends the non-js user to another page.

    this has been tested in FF3, S3, and IE8. I couldn't disable JS in chrome?!?!.

    pretty clever huh?

    of course if you're dumb enough to use xhtml 1.1 for making webpages, this might not work for you...
    Last edited by rnd me; 01-25-2010 at 08:02 PM.

  3. #18
    Join Date
    Jan 2010
    Posts
    3
    Ok..it works.. but it's a little bit weird hehe..

  4. #19
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by Kor View Post
    Nope. If javascript is enabled, the document will have no HEAD, which is HTML illegal. If you write another HEAD outside the NOSCRIPT tag, the document will have two HEADs in case javascript is disabled, which is HTML illegal as well.
    It isn't actually, but that's nitpicking. I agree with you fully that <noscript><head></head></noscript> is wrong.

  5. #20
    Join Date
    Sep 2011
    Posts
    2

    Using jQuery library

    The way I accomplish this is with jQuery. I simply set all the divs on a page to be hidden and then use jQuery to show the divs.

    Code:
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="testjs.js"></script>
    <div id="detectjs">
    <noscript><h1 style="color:red;">JAVASCRIPT MUST BE ENABLED TO USE THIS WEBSITE!</h1></noscript>
    </div>
    <div id="main" style="display:none;">
    ... CONTENT TO DISPLAY IF JS IS ENABLED ...
    </div>

    Then the jQuery file testjs.js:

    Code:
    $(document).ready(function(){
    	$("div#detectjs").hide();
    	$("div#entry").show();
            });

  6. #21
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by massspectrician View Post
    The way I accomplish this is with jQuery.
    That is redundant. JQuery is JavaScript, as it is nothing but a JavaScript library. Your example can simply be written in native JavaScript as:

    Code:
    <script type="text/javascript">
    onload=function(){
    document.getElementById('main').style.display='block';
    }
    </script>
    <noscript><h1 style="color:red;">JAVASCRIPT MUST BE ENABLED TO USE THIS WEBSITE!</h1></noscript>
    <div id="main" style="display:none">
    ... CONTENT TO DISPLAY IF JS IS ENABLED ...
    </div>

  7. #22
    Join Date
    Sep 2011
    Posts
    2
    That is redundant. JQuery is JavaScript, as it is nothing but a JavaScript library. Your example can simply be written in native JavaScript as:
    I am aware of this. I was only trying to provide an elegant alternative that works. I've always found "native" JavaScript to be cumbersome to code and the jQuery library has proven beneficial as a redux, thus I try to provide such.

    I puzzle at the notion that this is "redundant" as I've not seen this solution posted elsewhere on the thread. I've never claimed that jQuery was not JavaScript; nor would I.

  8. #23
    You can detect if the Javascript is disabled by NOSCRIPT .
    Use this code :
    <noscript>
    <meta HTTP-EQUIV="REFRESH" content="0;url=nonjavascriptpage.html">
    </noscript>

    For Javascript Redirection have a look at this javascript redirect tutorial : Page redirection with Javascript

    Good luck.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles