onLoad vs. browser type

Hi experts,

I need to onload different data based on different browsers. Here is an example.

if browser_type() == "Firefox"
<body onload="func_1(...)">
else if browser_type() == "MSIE"
<body onload="func_2(...)">


Can anybody tell me how to implement this? browser_type(), func_1(...) and func_2(...) are all javaScript
functions.

Thanks so much.
davidw88Asked:
Who is Participating?
 
hieloCommented:
test for "features" supported in IE that are not supported in other browsers. And don't use
<body onload="...">

use an onload function within a script tag. That way you can put all the js code in a separate js file when you are ready to deploy your project:

<html>
<head>
 <script>
window.onload=function()
{
  if("undefined"!=typeof(ActiveXObject) )
  {
   func_1();
  }
 else
 {
   //non-IE
  func_2();
 }

};
 </script>
</head>
<body>
</body>
</html>
0
 
davidw88Author Commented:
Thanks hielo for your reply. Let me test it and get back to you later.
0
 
fsze88Commented:
if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
  browserType = "MSIE";
  ie = true;   nn6 = false;
} else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
  browserType = "SAFARI";
  ie = false;   nn6 = true;
}else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
  browserType = "OPERA";
  ie = false;   nn6 = true;
}else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
  browserType = "FIREFOX";
  ie = false;   nn6 = true;
}else{
  browserType = "";
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ravi KallaTechnology LeadCommented:
below is the code the suits your requirement exactly
<script>
  function oneStop()
  {
    /* Logic for finding the browser type */
    if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
      browserType = "MSIE";
      ie = true;   nn6 = false;
    } else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
      browserType = "SAFARI";
      ie = false;   nn6 = true;
    }else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
      browserType = "OPERA";
      ie = false;   nn6 = true;
    }else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
      browserType = "FIREFOX";
      ie = false;   nn6 = true;
    }else{
      browserType = "";
    }
    alert(browserType);

    //Logic for calling methods according to the browser type
    /*
    if browserType == "Firefox"
	  func_1(...);
	else if browserType == "MSIE"
      func_2(...);
    */
  }
</script>
<body onload = "oneStop()">
  blah blah blah...
</body>

Open in new window

0
 
siddagrlCommented:
there is one more browser in the market :-)
==> Chrome

Also you will need to carefully order your if..else..conditions because userAgent of Chrome contains 'SAFARI' keyword!
see attached image...
ChromeUserAgent.JPG
0
 
davidw88Author Commented:
Thanks experts for your replies! I followed Ravi_kalla's idea to move forward. Now IE and Firefox work, however Chrome and Safari seem not work with <body onLoad=...>.

I have to use <body onLoad=...> as I need to pass arguments to javaScript functions.

Any more ideas?
0
 
siddagrlCommented:
see the attached code...
<html>
<head><title>Onload</title></head>
<body onLoad="callMe('this is my message')">

<script type="text/javascript">
function callMe(msg)
{
    alert(msg);
}
</script>

</body></html>

Open in new window

0
 
siddagrlCommented:
or this could be done using variables...
<html>
<head><title>Onload</title></head>
<body onLoad="callMe()">

<script type="text/javascript">
var m = 'this is my message';

//window.onload = callMe;

function callMe()
{
    alert(m);
}
</script>

</body></html>

Open in new window

0
 
davidw88Author Commented:
Thanks siddagrl. However, your reply does not answer my question.  What bothers me is that Chrome and Safari do not work with onLoad.  I know how to pass arguments to a javaScript function.

Specifically, Chrome and Safari do not work with
<body onLoad=...>


So, my question is: how to make Chrome and Safari work with onLoad?

0
 
hieloCommented:
>>I have to use <body onLoad=...> as I need to pass arguments to javaScript functions.
you are very confused. Using
window.onload=function(){ ... }

does NOT prevent you from calling functions that need parameters. Specifically instead of the "..." you need to determine which function(s) to call for which browser and pass the arguments as needed.

>>So, my question is: how to make Chrome and Safari work with onLoad?
try using onload (all lower case)
0
 
siddagrlCommented:
hi davidw88,

the code which i have posted works fine in Chrome 4.0.249.43 irrespective of case (onload or onLoad)
0
 
Ravi KallaTechnology LeadCommented:
this is not a big issue to identify the Chrome browser. Check for the string "Chrome" even before checking for another browsers. This resolves the issue. Below code works perfect.
<script>
  function oneStop()
  {
    /* Logic for finding the browser type */
    if (navigator.userAgent.toUpperCase().indexOf('CHROME')>=0){
      browserType = "CHROME";
      ie = false;   nn6 = true;
    }else if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
      browserType = "MSIE";
      ie = true;   nn6 = false;
    } else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
      browserType = "SAFARI";
      ie = false;   nn6 = true;
    }else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
      browserType = "OPERA";
      ie = false;   nn6 = true;
    }else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
      browserType = "FIREFOX";
      ie = false;   nn6 = true;
    }else{
      browserType = "";
    }
    alert(browserType);

    //Logic for calling methods according to the browser type
    /*
    if browserType == "Firefox"
          func_1(...);
        else if browserType == "MSIE"
      func_2(...);
    */
  }
</script>
<body onload = "oneStop()">
  blah blah blah...
</body>

Open in new window

0
 
davidw88Author Commented:
Thanks experts for all of your replies! Let me follow your ideas and test again.
0
 
davidw88Author Commented:
Hi experts,

I finally found the reason that "onLoad" did not work. There was "document.createElement(...)" in my script that Chrome and Safari seemed not be able to process. Chrome and Safari seem to handle "onLoad" in a different order from Firefox and IE.

All your replies are correct.  I give each reply 40 points.

Again, thanks experts for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.