Using PHP to detect JavaScript support

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

    #1

    Using PHP to detect JavaScript support

    I would like to use PHP (or any other server-side technique) to detect
    if JavaScript was turned on in the user's browser. Is there any known
    way to do this, and if not, why not!

    I realise I could serve one page to both, and have the basic function
    there by default and use DOM to replace it with another, but it would
    be more efficient and lower overhead to serve different pages to
    different UAs. Any suggestions welcome.

    Now if I was Galactic Overlord, browsers that supported JavaScript
    would be required to declare so in the Accept-Content HTTP header they
    send.
    Since I am not on usenet much, could anyone who replies pleace CC my
    email address?

    - Nick

  • Alvaro G Vicario

    #2
    Re: Using PHP to detect JavaScript support

    *** Nicholas Shanks wrote/escribió (Tue, 26 Apr 2005 11:56:41 +0100):[color=blue]
    > I would like to use PHP (or any other server-side technique) to detect
    > if JavaScript was turned on in the user's browser. Is there any known
    > way to do this, and if not, why not![/color]

    Use an initial test page where JavaScript creates a variable and sends it
    to server (hidden form field or URL attribute, cookie...) and read that
    value afterwards from PHP. Just be aware that cookies can be disabled and
    user may reach your page skipping the inicial test page.

    However, I'd say its a thousand times easier to just write code that works
    100% in server and use client-side JavaScript for UI enhancements. Why work
    twice?


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • Daniel Tryba

      #3
      Re: Using PHP to detect JavaScript support

      Nicholas Shanks <contact@nicksh anks.com> wrote:[color=blue]
      > I would like to use PHP (or any other server-side technique) to detect
      > if JavaScript was turned on in the user's browser. Is there any known
      > way to do this, and if not, why not![/color]

      No, because it is a clientside setting serverside environments don't
      know until told (by the client).
      [color=blue]
      > I realise I could serve one page to both[/color]

      How do you know what page to serve? That is what you are asking!
      [color=blue]
      > and have the basic function there by default and use DOM to replace it
      > with another[/color]

      What DOM (if any)?
      [color=blue]
      > but it would be more efficient and lower overhead to serve different
      > pages to different UAs. Any suggestions welcome.[/color]

      I beg to differ, serverside UA sniffing is almost always very poorly
      implemented (take a look at the browsercap function and have a good
      laugh). But than again clientside UA sniffing is also faulty in many
      cases.

      If you really want to server different pages to different available
      capabilites there is an easy way to distinguis between js and nojs:


      Comment

      • frizzle

        #4
        Re: Using PHP to detect JavaScript support

        You could use a button/link with a page in the href-part,
        and an e.g. onClick="docume nt.location.hre f='page2.php';r eturn false"
        or a document.write etc...

        Comment

        • Colin McKinnon

          #5
          Re: Using PHP to detect JavaScript support

          Alvaro G Vicario wrote:
          [color=blue]
          > *** Nicholas Shanks wrote/escribió (Tue, 26 Apr 2005 11:56:41 +0100):[color=green]
          >> I would like to use PHP (or any other server-side technique) to detect
          >> if JavaScript was turned on in the user's browser. Is there any known
          >> way to do this, and if not, why not![/color]
          >
          > Use an initial test page where JavaScript creates a variable and sends it[/color]
          <snip>

          Wouldn't it be simpler to just start each non-js page with js code to
          redirect to the js-enabled version. Point the user towards the non-js entry
          points and bingo.

          C.

          Comment

          Working...