SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    curious javascript question

    Hi there, I was just wondering if it was possible to detect if a user has javascript enabled or disabled, using javascript of course to find out? thanks.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,871
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Code:
    <p>JavaScript is <b id="js">not </b>enabled.</p>
    <script type="text/javascript">
    document.getElementById('js').display = 'none';
    </script>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <noscript>
    JavaScript is disabled.
    </noscript>

  4. #4
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if I asked my question right or not.

    basically its how, if possible can you use javascript to detect if the user has it enabled or not.

    ideally, I would want something to do where, I can detect if they have javascript enabled or not, then branching off to something else, the whole point being is, I cannot make a site, if their are people who visit websites without javascript, I can't rely in others words on javascript only, such as navigation to a different clone site based off of resolution, or changing image files based off browser.

  5. #5
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say there was a function in JS to detect if it was enabled or not, - it would never even run if JS was disabled so it loses its purpose!
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do realise, that your question is irrational, right?

    If you want to explicitly handle situations, where the client doesn't support javascript, you can use one of the suggestions, that have been given here. A better solution is to write your application in such a way, that it works without javascript, and then only use it for enhancing the experience, if supported. This technique is also known as unobtrusive javascript.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Just because you can't detect if javascript is enabled, doesn't mean that nothing can be done about it.

    If you're wanting one page that's used without javascript, and a completely different page that's used when there is javascript, you should start with the first page and use some script that takes them to the second page.

    Code JavaScript:
    window.location = "http://www.mysite.com/index-js.html";

    That's a poor way of doing it though, because you'll be having to maintain two different versions which always leads to trouble.

    Previously sites were developed with gracious degredation in mind, so that features would be added on to allow for the possibility that certain functionality may not be available.

    Now a much better technique is to use progressive enhancement, where you start with the presumption that only html can be seen, then add on css for styling, and further improve with javascript for behaviour.

    So instead of detecting if javascript is there, the basic presumption for the one page is that there is no javascript, but should there happen to be then we can enhance what's already on the page.

  8. #8
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha, I dont know what I was thinking really, I really never thought about it lol. but can you use a programming language or something to detect javascript is on or not tho?

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    If it's not then there is no programming language, so the answer is no.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,871
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    JavaScript is the only client side language that most browsers support and so there is no other language you can have the browser run to test if JavaScript is supported. The pieces of code kyberfabrikken and I posted earlier are the only two ways to change the content of the page based on whether or not JavaScript is enabled. In his example code the text is hidden when JavaScript is enabled due to its being enclosed in <noscript> tags. In my example the content is what you want it to read when JavaScript is not enabled and you use JavaScript to update that content when it is enabled (which does away with the need for the noscript tag and makes it more flexible as to when you update the content as you perform javaScript feature sensing first and treat browsers that don't support certain features as if they didn't support JavaScript at all.

    Internet Explorer supports vbScript and jScript instead of JavaScript (butJavaScript is mostly a sub-set of jScript allowing the same scripts to run on that browser as jScript as run on other browsers as JavaScript). You cannot however have either language enabled on IE without having both enabled and so trying to use vbScript to test whether or not jScript is enabled would be pointless since if vbScript is enabled in order to perform the test then jScript must also be enabled since there is only the one flag to turn activescripting (vbScript and jScript) on and off.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •