Toggle div based on java script

Discussion in 'JavaScript' started by alberti_n, Mar 12, 2007.

  1. #1
    I want to have a hidden div that is shown only to MSIE browsers, I assume this is pretty straight forward but i've never done it before. I already have a function that shows and hides a div based on a button which works fine as follows;

    <script language="javascript">
      function toggleDiv(divid){
        if(document.getElementById(divid).style.display == 'none'){
          document.getElementById(divid).style.display = 'block';
        }else{
          document.getElementById(divid).style.display = 'none';
        }
      }
    </script>
    Code (markup):
    I though i'd be able to do something like this;

    <script type="text/javascript">
    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
    temp=navigator.appVersion.split("MSIE")
    version=parseFloat(temp[1])
    }
    
    if (version>=5.5) 
    toggleDiv('alert') //alert is the div I wish to reveal
    </script>
    Code (markup):
    but it doesnt seem to work. I tested it with the button and it shows correctly, this div is as follows;

      <div id="alert" style="display:none">
            <p class="alert">Lorem ipsum dolor sit amet.</p>
      </div>
    Code (markup):
    Thanks for your help :)
     
    alberti_n, Mar 12, 2007 IP
  2. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make sure you have the javascript after you define the div. Because web pages are read top to bottom.
     
    giraph, Mar 12, 2007 IP
  3. alberti_n

    alberti_n Active Member

    Messages:
    75
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #3
    ok..makes sense :)

    I think I am getting there.... but if i have the following;

    
    if (version>=5.5) //NON IE browser will return 0
    document.write("MSIE 5.5+!!")
    toggleDiv('alert')
    </script>
    Code (markup):
    I see the comment "MSIE 5.5+!!" in the page (when I use IE) but the div does not show up.
     
    alberti_n, Mar 12, 2007 IP
  4. alberti_n

    alberti_n Active Member

    Messages:
    75
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #4
    I gave up and did it in PHP much nicer :)
     
    alberti_n, Mar 12, 2007 IP