SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking in PHP if Javascript is available

    I'm working on the non-Javascript compatibility of my site, and am stumped on one part.

    Right now, I need to choose one out of two functions to call, depending on whether or not Javascript is enabled. The problem is... I can't find a way to know that in PHP.

    I know of the noscript-tags, but a simple test like this doesn't work:
    Code PHP:
    <?php
    $javascript = true;
    ?>
    <noscript>
    <?php $javascript = false; ?>
    </noscript>
    Here, $javascript will always return false, so this isn't an option.

    Another way's by setting a hidden field in a form, altering it with Javascript, then checking its value in PHP. Problem with that is that I need to know without resending the page, so this isn't an option either.

    Third possibility that I found, was the get_browser() function. Only thing with that, is that it shows whether or not Javascript is possible on that browser, not if it's curently enabled or not.

    So... I'm stumped. Does anybody know how to do this?

  2. #2
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can't be done as far as im aware, you can use php to determine if the browse HAS javascript, but not if its enabled or not (as its clientside)

    http://bytes.com/groups/php/541838-h...pt-enabled-php had some interesting points

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You can't. Why would you need that? Why would you want to call a different function server side depending on what is activated client side?

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd want that to set up an either Javascript-heavy image gallery, or a light, pure PHP/HTML one without fading effects and auto-scrolling. A simple noscript-tag won't help.

    I managed to slap a function for this together, which comes along with a session var. When you do have Javascript enabled, it gives a quick flash of white, though, which kinda sucks.

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Progressive enhancement should happen on the client, rather than the server side. What I mean is you should be able to serve the same markup to all users, and the Javascript then highjacks links, installs listeners, does it's fades and animations, hides irrelevant sections (by class name) and adds its functionality on top of the basic markup.

    If it's really too hard to use the same underlying markup the only way to get PHP to know about JS is to have JS embed some data into a form that gets submitted (e.g. a login form, and remember the result for the session) or to send an ajax request.
    You could server the no-frills markup by default then have JS completely replace it with an XHR call, but that's a bit wasteful as most users will be downloading that part twice.


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
  •