Re: Javascript detection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 On Thursday, April 28, 2011, tedd wrote:
 
> To answer your question in a new thread.

> No, the $_SERVER super-global isn't going to give you anything nor is 
> anything else like it.

> You see, PHP has a difficult time detecting IF Javascript is turned 
> ON in the client's browser because PHP is history by the time the 
> browser does anything, including running Javascript.

Thanks, Tedd. That's how I suspected things to be. However, the list
was very quiet, I might have missed something, and so I felt it
couldn't hurt to ask. TBH I'd already done:

 echo "<pre>";
 print_r ($_SERVER);
 echo "</pre>";

and didn't spot anything there that I thought would give me the info I
sought - but then I don't know the purpose of every element of that
array and $_SERVER is not the only superglobal!

> As Yogi Berra once said; "It's always hard to predict things 
> especially when it deals with the future."

However, once the browser has requested a transfer via HTTP that
request is in the past and that request is where stuff that lets you
know some of the browser's capabilities (e.g. HTTP_ACCEPT) comes from.
So it would be possible by a similar mechanism for a browser to tell
your script whether or not Javascript (or any other language for that)
is available.

> However, there are two ways to "kind-of" doing it:

> Way 1 --  I place an element in html that is hidden from the user's 
> view via css (display:none) and if Javascript is ON then Javascript 
> changes the css so that the element is shown to the user 
> (display:block). Here's an example:

This is the reverse of part of what I've done for the page in
question. I had a tabbed interface similar to:

<div id="sect01" style="display:block;">
[stuff]
</div>
<div id="sect02" style="display:none;visibility:hidden;">
[stuff]
</div>
...

However, the second div must be visible if JS isn't available
otherwise the user can't access that section. So I've changed the
style of the second and subsequent divs to display:block and then used
an onLoad routine to change the .style.display and .style.visibility
attributes of the second and subsequent divs to none and hidden
respectively so that those who have JS get the full DHTML.

Now I also using AJAX on that page. However, I can use the fact that
the trigger events aren't handled if JS isn't available to just
retrieve what's actually needed at the time via AJAX if JS is
available or submit the full form if it's not. Of course, that
complicates things in the PHP on the server as the script then has to
handle both interim and final form submission.

FWIW, it's possible to detect whether or not Javascript is available,
but not AFAICT at 'first contact' because you need the 'first contact'
page to do something to prove that JS is available, from which you can
assume that JS is not should that something not be done. For example,
you can make the link to a page into a form submission - e.g:

  <form name='jstest' action='myscript.php' method='post'>
  <input type='hidden' name='wehavejs' value=1>
  </form>
  <a href='myscript.php' onClick="document.forms['jstest'].submit();return(false);">
  Click Here</a>

The form is submitted if the browser has JS and so the hidden input
field is posted. However, if the browser doesn't have JS the default
behaviour occurs when the link is clicked and so the field is not
posted. Hence we can use isset($_POST['wehavejs']) to determine
whether or not the browser has JS capability.

Thanks again,

-- 
Geoff


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux