SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    What is wrong with this Javascript code?

    Hi,

    I am trying to use Javascript to sniff the browser and if it is IE then display one div "ie" and if not IE then display div "non-ie".

    But the If else part that contains the actual command to display a given div is not firing at all. As you can see I have put a alert command in each section just for debugging purposes and these alerts dont even fire! So what is wrong with this JavaScript please:

    Code JavaScript:
    <script language="JavaScript">
     
    var isIE = window.ActiveXObject ? 1 : 0;
     
    alert("Is Browser IE: " + isIE);
     
    if (isIE == 1)
      {
      // code for IE6, IE5
      document.getElementById('ie').style.display = 'block';
      alert("Browser is IE");
     
      } else if (isIE == 0) {
     
      // code for Firefox, Chrome, Opera, Safari
      document.getElementById('non-ie').style.display = 'block';
      alert("Browser is NOT IE");
     
      }
     
    </script>


    ThanX,
    Dean.

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any console error messages?

    This in-line code must not precede the elements to which it refers.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No, "console error messages".
    I did not even think such an error message was possible in case of Javascript.


    Quote Originally Posted by Logic Ali View Post
    Any console error messages?

    This in-line code must not precede the elements to which it refers.

    What does this mean?
    This Javascript code is where we always put Javascript codes, that is between <head></head>

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Normally there are cross-browser techniques that, depending on what you want to achieve, work well across all browsers. You really should investigate those instead.

    Detecting browser types is a technique that belongs in the last century, and should remain there.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the sage advice

    But can you tell me why the code between the IF Else is not firing at all?
    I mean the code above it is firing, but not the code between the IF Else, not even to the point of the alerts firing!

    Quote Originally Posted by pmw57 View Post
    Normally there are cross-browser techniques that, depending on what you want to achieve, work well across all browsers. You really should investigate those instead.

    Detecting browser types is a technique that belongs in the last century, and should remain there.

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Thanks for the sage advice

    But can you tell me why the code between the IF Else is not firing at all?
    I mean the code above it is firing, but not the code between the IF Else, not even to the point of the alerts firing!
    Do you have elements on the page with the appropriate unique identifiers? Or perhaps, are you running the script before those parts of the page exist?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well I must say this was a good learning experience.
    That is the reason that Javascript was not running was that the code that turned on the div element was loaded before the body of the page, so it was not running because I guess the way JS is designed is that the divs must load 1st before it can see the divs!

    So I moved the Javascript to after the body, and now it works fine.

    Very interesting!

    Thanx for your tip.
    Dean.

    Quote Originally Posted by pmw57 View Post
    Do you have elements on the page with the appropriate unique identifiers? Or perhaps, are you running the script before those parts of the page exist?

    Anoox search engine volunteer

    www.anoox.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •