• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaScript for Multiple browsers

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • 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
  • 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');
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't support such browsers. Which browsers are you still trying to support that require that?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic