Reply to Re: XMLHttpRequest and AJAX for PHP programmers

Your name:

Reply:


Posted by Erwin Moller on 03/27/07 12:51

geevaa wrote:

<snip>

> Determining what browser you are working with and creating the
> appropriate object is simple:
>
> [javascript code]
> /* The following function creates an XMLHttpRequest object... */
>
> function createRequestObject(){
> var request_o; //declare the variable to hold the object.
> var browser = navigator.appName; //find the browser name
> if(browser == "Microsoft Internet Explorer"){
> /* Create the object using MSIE's method */
> request_o = new ActiveXObject("Microsoft.XMLHTTP");
> }else{
> /* Create the object using other browser's method */
> request_o = new XMLHttpRequest();
> }
> return request_o; //return the object
>
> }
>
> /* You can get more specific with version information by using
> parseInt(navigator.appVersion)
> Which will extract an integer value containing the version
> of the browser being used.
> */

Hi,

This is getting totally off topic (PHP), but I must make a little
correction.
The above method of getting the browserspecific XMLHTTP-thingy is well...
bad.
Browsersniffing, as the above code does, is frowned upon by the better part
of the JavaScript-community.
It is much better to simply test for the existance of the needed object
instead of branching on appName and appVersion.

Similar to the test if the browser support images:
if (document.images){
alert("You have imagesupport.");
} else {
alert("You have NO imagesupport. Are you still running LYNX?");
alert("By the way: Does LYNX support Javascript at all?");
}

You should do the same with the XMLhttp-thing.

Avoiding browsersniffing saves you a headache too. Like the many what-if
situations like: IE, but without scripted ActiveX, or older versions, etc.
Simply test if the object is supported by the browser keeps things clean and
simple.
It is also your best bet that your script keeps working if new
browserversions (or browsers) are released.

www.w3schools.com/ajax has a good example of how to get that object, simply
by testing the existance of the various flavors around, and returning them
on succes.

Just my 2 cent..

Regards,
Erwin Moller

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация