Detecting if JavaScript is enabled

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anic297

    Detecting if JavaScript is enabled

    Hi,

    This is certainly a simple question, but I'm a newbie in JavaScript.

    Is there a way to know if JavaScript is enabled?

    My php script uses JavasSript to do something. If JavaScript is not
    enabled, I can put a "submit" push button. But I don't want both (the
    "submit" push button with the automatic action), so I want to know on
    the fly which one I must use (whether JavaScript is enabled).
  • Evertjan.

    #2
    Re: Detecting if JavaScript is enabled

    Anic297 wrote on 20 aug 2008 in comp.lang.javas cript:
    Hi,
    >
    This is certainly a simple question, but I'm a newbie in JavaScript.
    >
    Is there a way to know if JavaScript is enabled?
    <script type='text/javascript'>
    document.write( 'Javascript is enabled');
    </script>
    My php script uses JavasSript to do something.
    Impossible, as PHP only runs serverside, and Javascript runs clientside.

    [Well it can run serverside on ASP, but not on PHP]
    If JavaScript is not
    enabled, I can put a "submit" push button. But I don't want both (the
    "submit" push button with the automatic action),

    <input type='submit' id='myButton'>
    <script type='text/javascript'>
    document.getele mentById('myBut ton').style.dis play = 'none';
    </script>
    so I want to know on
    the fly which one I must use (whether JavaScript is enabled).
    That is not about you knowing, but about programming for it.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Detecting if JavaScript is enabled

      Anic297 wrote:
      Is there a way to know if JavaScript is enabled?
      I can write you a script if you can write me a program that detects if the
      computer is running that the program is running on.
      My php script uses JavasSript to do something.
      That's unlikely.
      If JavaScript is not enabled, I can put a "submit" push button. But I
      don't want both (the "submit" push button with the automatic action), so
      I want to know on the fly which one I must use (whether JavaScript is
      enabled).
      <form action="foo.php " onsubmit="...; return false">
      ...
      <input type="submit" ...>
      ...
      </form>

      This was a FAQ, please search the archives before you post.
      <http://jibbering.com/faq/>


      PointedEars
      --
      realism: HTML 4.01 Strict
      evangelism: XHTML 1.0 Strict
      madness: XHTML 1.1 as application/xhtml+xml
      -- Bjoern Hoehrmann

      Comment

      • Anic297

        #4
        Re: Detecting if JavaScript is enabled

        Evertjan. a écrit:
        >My php script uses JavasSript to do something.
        >
        Impossible, as PHP only runs serverside, and Javascript runs clientside.
        Yes... I have to learn that...
        >If JavaScript is not
        >enabled, I can put a "submit" push button. But I don't want both (the
        >"submit" push button with the automatic action),
        >
        <input type='submit' id='myButton'>
        <script type='text/javascript'>
        document.getele mentById('myBut ton').style.dis play = 'none';
        </script>
        Oh, yes, hiding the button from JavaScript only if it's enabled!
        >so I want to know on
        >the fly which one I must use (whether JavaScript is enabled).
        >
        That is not about you knowing, but about programming for it.
        Well, I do desktop programming, but web programming is not the same thing!

        Thank you!

        Comment

        • Anic297

          #5
          Re: Detecting if JavaScript is enabled

          Thomas 'PointedEars' Lahn a écrit:
          >Is there a way to know if JavaScript is enabled?
          >
          I can write you a script if you can write me a program that detects if the
          computer is running that the program is running on.
          Well... perhaps not a JavaScript script...
          (but I have had my answer)
          >My php script uses JavasSript to do something.
          >
          That's unlikely.
          Well, I don't use correct terms (I'm new in web programming). I just
          expected readers to understand my phrases.
          This was a FAQ, please search the archives before you post.
          <http://jibbering.com/faq/>
          Thanks

          Comment

          • david.karr

            #6
            Re: Detecting if JavaScript is enabled

            Anic297 wrote:
            Hi,
            >
            This is certainly a simple question, but I'm a newbie in JavaScript.
            >
            Is there a way to know if JavaScript is enabled?
            Ignoring the details of what you'd do on the server side with this
            knowledge, and even whether knowing this is generally useful, and your
            application can work with an initial form that doesn't need to
            specifically know whether JavaScript is available or not, you could
            then have a script that runs on the page that uses the DOM api to add
            a hidden input element to the form, where the hidden value indicates
            that JavaScript is present. When the form is submitted, either by the
            user or by JavaScript, the server-side code would then be informed
            whether the browser had JavaScript enabled.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Detecting if JavaScript is enabled

              Anic297 wrote:
              Evertjan. a écrit:
              >>My php script uses JavasSript to do something.
              >Impossible, as PHP only runs serverside, and Javascript runs clientside.
              >
              Yes... I have to learn that...
              And to unlearn it afterwards. Neither language is restricted to client-side
              oder server-side use. It is only that PHP is more often used server-side,
              while ECMAScript implementations are probably more often used client-side.
              >>If JavaScript is not
              >>enabled, I can put a "submit" push button. But I don't want both (the
              >>"submit" push button with the automatic action),
              ><input type='submit' id='myButton'>
              ><script type='text/javascript'>
              > document.getele mentById('myBut ton').style.dis play = 'none';
              document.getEle mentById(...).. .

              But document.forms[...].elements["myButton"] is more compatible.
              ></script>
              >
              Oh, yes, hiding the button from JavaScript only if it's enabled!
              This solution can be recommended against as it introduces a dependency on
              the CSS DOM needlessly.


              PointedEars
              --
              Anyone who slaps a 'this page is best viewed with Browser X' label on
              a Web page appears to be yearning for the bad old days, before the Web,
              when you had very little chance of reading a document written on another
              computer, another word processor, or another network. -- Tim Berners-Lee

              Comment

              • Anic297

                #8
                Re: Detecting if JavaScript is enabled

                Evertjan. a écrit:
                <input type='submit' id='myButton'>
                <script type='text/javascript'>
                document.getele mentById('myBut ton').style.dis play = 'none';
                </script>
                Hi,
                Despite the fact that the code is simple, I can't get it to work.
                Here's the source code from my browser:
                Select a QuickTime Movie:
                <FORM ACTION="./moviesindex.php ?movieskind=qt" METHOD="post" NAME="nav2">
                <SELECT NAME="url"
                onChange="docum ent.location.hr ef=document.nav 2.url.options[document.nav2.u rl.selectedInde x].value">
                <OPTION VALUE=./moviesindex.php ?movieskind=qt& moviename=mov1> Mouth
                <OPTION VALUE=./moviesindex.php ?movieskind=qt& moviename=mov2> Eyes
                .... (other options)
                </SELECT>

                <INPUT TYPE="SUBMIT" id="MovieButton " VALUE="Clic here if the page does
                not update">

                <script type='text/javascript'>
                document.getele mentById("Movie Button").style. display = "none";
                </script>
                </FORM>

                I think it should not make any difference (since the code above is the
                result), but all is done in php (I send all using the "echo" command).

                I also tried using "<script language='Javas cript'>".

                It's hard when you know programming but not web programming: you know
                logical ways of doing things, but the syntax and principles are totally
                differents (I start from scratch)!

                What do you suggest?

                Comment

                • Anic297

                  #9
                  Re: Detecting if JavaScript is enabled

                  Thomas 'PointedEars' Lahn a écrit:
                  And to unlearn it afterwards. Neither language is restricted to client-side
                  oder server-side use. It is only that PHP is more often used server-side,
                  while ECMAScript implementations are probably more often used client-side.
                  Ok...
                  >> document.getele mentById('myBut ton').style.dis play = 'none';
                  >
                  document.getEle mentById(...).. .
                  You know, I had replied to "Evertjan." 's post because it didn't worked
                  (even with differences). Now, I have removed my unnecessary post ;-)
                  But document.forms[...].elements["myButton"] is more compatible.
                  Ok...
                  >Oh, yes, hiding the button from JavaScript only if it's enabled!
                  >
                  This solution can be recommended against as it introduces a dependency on
                  the CSS DOM needlessly.
                  I think it's an interessant approach, anyway.

                  Thank you for all this!

                  Comment

                  • Dr_KralNOSPAM@nyc.rr.com

                    #10
                    Re: Detecting if JavaScript is enabled

                    If JS is NOT enabled there is nothing you can do in the client side so you
                    must write your code assuming it is off.

                    Have this <body onload='js_exis t_code' Then if JS is active (on) the
                    function 'js_exist_code' runs and changes stuff to the JS version. A lot
                    can be done by just using style stuff and changing className of some
                    elements.

                    This fails if the user turn JS after loading. <g>

                    HTH

                    Comment

                    • Anic297

                      #11
                      Re: Detecting if JavaScript is enabled

                      david.karr a écrit:
                      You could then have a script that runs on the page that uses the DOM api
                      to add a hidden input element to the form, where the hidden value
                      indicates that JavaScript is present. When the form is submitted,
                      either by the
                      user or by JavaScript, the server-side code would then be informed
                      whether the browser had JavaScript enabled.
                      Yes, that would work if I want to know the enabled state of JavaScript
                      when data are submitted. In my case, I want to know that when the page
                      is being written.

                      Thanks, it will be good to know for the future.

                      Comment

                      • Anic297

                        #12
                        Re: Detecting if JavaScript is enabled

                        Dr_KralNOSPAM@n yc.rr.com a écrit:
                        If JS is NOT enabled there is nothing you can do in the client side so you
                        must write your code assuming it is off.
                        Well, I have an alternative (either it's called by an event in
                        JavaScript or a push button appears). In this case, I think it's good to
                        know.
                        Have this <body onload='js_exis t_code' Then if JS is active (on) the
                        function 'js_exist_code' runs and changes stuff to the JS version. A lot
                        can be done by just using style stuff and changing className of some
                        elements.
                        Hmmm... I'm not yet at this degree of knowledge ;-)
                        This fails if the user turn JS after loading. <g>
                        Good point, indeed.
                        (in my actual case, it's worse if the user turns JS on, not off, after
                        loading.
                        Thank you.

                        Comment

                        • Anic297

                          #13
                          Re: Detecting if JavaScript is enabled

                          Evertjan. a écrit:
                          <input type='submit' id='myButton'>
                          <script type='text/javascript'>
                          document.getele mentById('myBut ton').style.dis play = 'none';
                          </script>
                          Yes, it works (as expected)! Thank you very much!

                          Comment

                          Working...