• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript for Multiple browsers

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

How do we write JavaScript code for multiple browsers? Old and new (Mozilla, IE etc...)? I feel the browser type is checked and the code is written like with if else statements. But say we have 20 new browsers in the market what would happen then? Also, how can we execute JavaScript on old browsers which do not support it?

Thanks.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um... I think, I found the answer.

if (window.XMLHttpRequest){
// browser is Firefox, Netscape, etc.
objXML=new XMLHttpRequest();
}
else if(window.ActiveXObject){
// browser is Internet Explorer
objXML=new ActiveXObject('Microsoft.XMLHTTP');
}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Capability detection (as shown in your example), rather than browser detection, is highly preferable.

Or better yet, use a library like jQuery where someone has already done the work for you.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Capability detection (as shown in your example), rather than browser detection, is highly preferable.

Or better yet, use a library like jQuery where someone has already done the work for you.



Ok, And for old browsers, we cannot do anything but write our javascript code in the following way.

<script type="text/javascript">
<!-- hide script from older browsers

---- Your JavaScript functions here ----

// -->
</script>

The old browsers will comment out everything between <!-- and --> ignoring the javascript commnets //. New browsers will realize that // is a javascript comment and execute the javascript code.

Correct?
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote: Or better yet, use a library like jQuery where someone has already done the work for you.



How does jQuery work on browser compatibility Bibeault?

Thanks.


 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't support such browsers. Which browsers are you still trying to support that require that?
reply
    Bookmark Topic Watch Topic
  • New Topic