#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    The US of A
    Posts
    9
    Rep Power
    0

    Detecting Javascript with PHP


    Is there any way to detect if the users browser is javascript enabled through php? I was looking through a couple of sites, and could not find anything.
  2. #2
  3. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    No, because it is a client side technology (same thing for Flash). If you want, you can map the browser name from getenv("HTTP_USER_AGENT") against a list of JavaScript enabled/disabled browsers that you could build up.
    Alex
    (http://www.alex-greg.com)
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    The US of A
    Posts
    9
    Rep Power
    0
    Ok. Thanks!
  6. #4
  7. Second highest poster :p
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2001
    Posts
    7,322
    Rep Power
    33
    The problem with that is i can still turn off javascript in the new browsers. What i would do is use a javascript redirect on a loading page and if the redirect goes through you know they have javascript.

    Code:
    <script language="Javascript">
      window.location="http://domain.com/index.php?js=true";
    </script>
    - Andreas Koepke

    Koepke Photography

  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    121
    Rep Power
    13
    Originally posted by a.koepke
    The problem with that is i can still turn off javascript in the new browsers. What i would do is use a javascript redirect on a loading page and if the redirect goes through you know they have javascript.

    Code:
    <script language="Javascript">
      window.location="http://domain.com/index.php?js=true";
    </script>
    That seems to be the only solution :/ Problem is, users also bookmark your page, they dont come through an entry page everytime.
  10. #6
  11. Second highest poster :p
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2001
    Posts
    7,322
    Rep Power
    33
    Yeah and if javascript gets annoying they can turn it off. I use the Prefs Bar for Mozilla and have a tick box to turn off Javascript instantly. Easy
  12. #7
  13. Muhhnnn !!
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2003
    Posts
    1,530
    Rep Power
    89
    to avoid the problem of bookmark, then you still can check referer... if referer is not your page and js=true then call the redirection again =P
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    34
    Rep Power
    12
    the referer is not always known. It's not known if it's a bookmarked url that's being called, but even if you go through the normal procedure of visiting someone's page the referer is not always know, so I wouldn't use the referer thingy as visitors may be unable to visit your site.

    maybe it would work to have an entry page with some hidden form and some auto-submit thingy with javascript that redirects you. That way some POST variable would be known which could be checked for in the URL that is defined as target.

    or you could use the startpage to set some sort of session with php execute the javascript redirect automatically and the target page can check if the session is set

    okay, I'm having wild ideas, but they avoid the problems with the http_referer and the bookmarked url I think.
    Last edited by Ssy; June 27th, 2003 at 08:24 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    WNY
    Posts
    207
    Rep Power
    12
    how about using the <noscript> tag for those browsers that have js disabled? like so:
    Code:
    <script language="Javascript">
      window.location="http://domain.com/index.php?js=true";
    </script>
    <noscript>
      <meta http-equiv="refresh" content="3; url=http://domain.com/index.php?js=false" />
    </noscript>
    That way you will get an answer either way. Then use sessions (and/or cookies) to store the result. If a session is already started, just check it on every page for the status of js for that users session. However, if no session exists for this user (say they bookmarked an inner page yeasterday and came directly to it today) start a session and use php to output the above code to the page. That way, the only time the refresh will happen is when a session is started.

    Consider the benefits
    - the redirect page will only contain a few lines of text and will load quickly - no long waits.
    - the redirect will only occur once for each users visit to the entire site.
    - it does not matter how or where the user enters the site.
    - you can provide pages optimized for the user's browser settings (js or no js), enhancing there experience.

    Wowww, scared myself with that last one. I think you get the idea.

    Note: I have never actualy used or tested this method. It just occured to me while I was reading this thread. Maybe it will work and maybe it won't
    Now I'm hungery! Lunch time!!
    strrev ("grebmil")

IMN logo majestic logo threadwatch logo seochat tools logo