Best way to deal with Javascript (in)compatibility using PHP?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

We have started off using a $_GET parameter to keep track of the user's

We detect what browser the visitor is using when he first arrives on
our website then we do a redirect to the same page adding on
"&browser=IE" or "&browser=DOM" at the end of the url (and we keep the
browser parameter in the URL the whole time he is on the website), this
enables us to use the following PHP method:

function javascript($name) {
  if($_GET[brow] == "IE") {
    return "document.all('$name')";
  else {
    return "document.getElementById('$name')";

That we use like this (for example here we are setting the inner HTML
for the item whose ID is 'contribute'):

<?=javascript('contribute')?>.innerHTML = "Name";

Everything was working fine but we have realised that the Googlebot
cannot deal with $_GET parameters so we are looking into url rewriting
but it is going to be very complicated to keep the method we are using
with url rewriting.

We would be interested in hearing about the way people deal with
javascript browser compatibility using PHP. There must be some much
simpler ways of doing this but we are not sure how to do it.
Any input would be helpful.


Re: Best way to deal with Javascript (in)compatibility using PHP?

Quoted text here. Click to load it

I'm not sure to understand which is the problem with  
document.getElementById for you (it should work on almost all present  
and future browsers) but try to read this page:

Re: Best way to deal with Javascript (in)compatibility using PHP?

"" wrote:

Quoted text here. Click to load it

Bad idea. Unless you don't really care what happens when people share links
to your site, or visit your site from search engines.

Quoted text here. Click to load it

That is probably going to damage your search engine ranking.

Quoted text here. Click to load it

Has it not occurred to you that you can do all of this in Javascript?

function getObject(n) {
  if (document.all) return document.all(n);
  else return document.getElementById(n);

Here are a couple of links for you:

phil [dot] ronan @ virgin [dot] net

Re: Best way to deal with Javascript (in)compatibility using PHP?

On 29/06/2005 13:39, Philip Ronan wrote:


Quoted text here. Click to load it

The term, object detection, probably isn't the best one to use. It has  
connotations with 'Browser detection via object inference', as you might  
notice by looking at some of the search results. A better phrase to  
consider is feature detection, and it is this principle that the OP  
should be trying to embrace.

The actual browser that is in use should normally be of absolutely /no/  
concern. The only interest should be what the capabilities are of that  
browser (hence /feature/ detection).

Feature detection typically involves a one-to-one analysis of the  
environment. Simply assuming that because the host supports - for  
instance, document.getElementById - that it must automatically support  
the entire W3C DOM Core module is clearly flawed (though that's what  
many seem to do). Instead, one determines what is necessary to perform a  
particular task, and then sets about finding if those methods and  
objects are indeed present in the host environment. Doing this in an  
efficient manner typically requires a thorough understanding of ECMAScript.

The FAQ notes for comp.lang.javascript gives a more in-depth treatment  
of why the various detection methods are flawed, and why feature  
detection is vastly superior (including some simpler examples). If you  
have particular issues, post there.

See <URL:


Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Re: Best way to deal with Javascript (in)compatibility using PHP?

Avoid URL rewriting altogether. I think there are more problems
associated with this than you realize.

Set a $_SESSION variable. Check/set it at the beginning of every page.

if (isset($_SESSION['browser']) == false) $_SESSION['browser'] =

This will allow the rest of your adaptive code to still work, without
constantly having to explicitly pass around something that should be
the same on every single page during a session.


Re: Best way to deal with Javascript (in)compatibility using PHP? wrote:

Quoted text here. Click to load it

If find that putting this function into your javascript library solves the

function ob(oname) {
  if (document.getElementById)
   return document.getElementById(oname);
  else if (document.all)  
   return document.all[name];

Once this is in there, you can write universal code that just refers to
objects using the "ob('id')" function.  Note that it is going by ID, not by

Kenneth Downs
Secure Data Software, Inc.

Re: Best way to deal with Javascript (in)compatibility using PHP?


on 06/29/2005 09:24 AM said the following:
Quoted text here. Click to load it

Often is not a matter of dealing with browser differences but also  
different Javascript versions in different versions of the same browser.

The most reliable way to solve that problem is to provide alternate  
Javascript code that is conditionally executed at Javascript run time  
depending on what the browser supports.

That is the solution used by this form validation class for instance to  
validate e-mail addresses. If the regular expression objects are  
available, it uses them, otherwise it uses a more basic type of e-mail  
address validation:

    return ( RegExp('email-regular-expression','gi'))>=0)
    some other type of basic validation;

Regular expression detection support is made checking the existence of  
the search method in the string object.


Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP

PHP Reviews - Reviews of PHP books and other products

Metastorage - Data object relational mapping layer generator

Site Timeline