wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes JavaScript for Multiple browsers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JavaScript for Multiple browsers" Watch "JavaScript for Multiple browsers" New topic
Author

JavaScript for Multiple browsers

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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.

Be Humble... Be Nice.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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

Joined: Nov 10, 2007
Posts: 627
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

I don't support such browsers. Which browsers are you still trying to support that require that?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript for Multiple browsers