noscript, onload or something else?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UberGirl
    New Member
    • Aug 2009
    • 9

    #1

    noscript, onload or something else?

    Hi,
    I'm a novice... just trying figure out something simple, I'm sure.

    I've got a script within my <head> tags to show/hide divs on click:

    Code:
    function showHide(divID, imgID){
    	if (document.getElementById(divID).style.display == "none") {
    		document.getElementById(divID).style.display = "block";
    		document.getElementById(imgID).src = "http://bytes.com/images/collapse.gif";
    	} else {
    		document.getElementById(divID).style.display = "none";
    		document.getElementById(imgID).src = "http://bytes.com/images/expand.gif";
    	}
    }
    Using it as follows:

    Code:
    <div class="areacode"><img id="416image" src="http://bytes.com/images/expand.gif" onclick="showHide('416',this.id)"/> AREA CODE 416</div>
    
    <div id="416" style="display:none">
    etc.

    I was going to use <noscript> to expand all of the divs by default if the user had javascript disabled, but I've read that's the wrong way to go, so then I was going to reverse the layout to have the divs expanded by default (i.e. display:block) and then use script to collapse them on page load.

    But I can't figure out if I can use this with onload="showHid e()" or something else. I want to be able to collapse all of the divs when the page loads, but my script is designed to expand/collapse an individual div on click. I've tried a few things but I'm terrible with syntax and I keep getting errors.

    Suggestions?
    Last edited by gits; Aug 12 '09, 10:21 AM. Reason: fix bold tags -> to [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can get all divs by a common class name and apply the function to it. needs some minor change in the code to reflect the use of this.

    (out of time…)

    Comment

    • UberGirl
      New Member
      • Aug 2009
      • 9

      #3
      Thanks, Dormilich.

      I was able to search out some examples using className and I got it to work.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        keep in mind that document.getEle mentsByClassNam e() is not fully supported in every version of IE.

        Comment

        • UberGirl
          New Member
          • Aug 2009
          • 9

          #5
          hide is working, but changing image is not

          I ended up using the code below, calling HideDiv and ToggleDivImage onload, but only hiding the divs on load (function toggledisplay) is working. The function toggleimage isn't, though the alerts I put in place verify that the variable "image" ends up getting changed from collapse.gif to expand.gif. Yet the result isn't reflected on the page (i.e. collapse.gif still shows).

          What am I doing wrong?


          Code:
          function toggledisplay(elem) {
          visibility = elem.style;
          if(visibility.display=='block')
          visibility.display = 'none';
          }
          
          function toggleimage(elem) {
          image = elem.src;
          if(image=='collapse.gif')
          alert("Should replace image");
          image = 'expand.gif';
          alert(image);
          }
          
          function HideDiv() {
          var boxes = document.getElementsByTagName("div");
          for(var i=0;i<boxes.length;i++) 
          if(boxes[i].className.match("hidediv")) toggledisplay(boxes[i]);
          }
          
          function ToggleDivImage() {
          var boxes = document.getElementsByTagName("img");
          for(var i=0;i<boxes.length;i++) 
          if(boxes[i].className.match("changeimage")) toggleimage(boxes[i]);
          }

          Comment

          • UberGirl
            New Member
            • Aug 2009
            • 9

            #6
            Anyone have any ideas on what I need to do to get the image swapped from collapse to expand during body onload?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by UberGirl
              to get the image swapped from collapse to expand during body onload?
              I don’t recommend meddling with the rendering during page load. wait until it’s done and you can be sure that everything is in place (meaning, you can’t do something with images that haven’t been loaded yet).—but that’s probably picking on words…

              could you explain to me what the javascript is supposed to do (there is no event related code shown) (what function to call when)?

              Comment

              • UberGirl
                New Member
                • Aug 2009
                • 9

                #8
                okay - I think I understand. so I need to try to swap them after they load - which would mean calling a script after that point in the body or at the end of the body? sorry - I really am new at this.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Originally posted by UberGirl
                  so I need to try to swap them after they load - which would mean calling a script after that point in the body or at the end of the body?
                  you can either call that script at the end of the body or use the body’s onload event, the latter having the advantage that you can place it in an external file.

                  you can even call it after the point in the body though I don’t recommend that, because you may want to access html elements later in the body (which will obviously not yet exists). if you immediately need to show the result (aka during load) you should think of appropriate default values for the html elements.

                  Comment

                  • UberGirl
                    New Member
                    • Aug 2009
                    • 9

                    #10
                    But my problem is that the default values (i.e. if the user has scripts disabled) needs to be the collapse image, which matches the default value of the expanded divs.

                    I thought you said I shouldn't do it onload because the page hasn't loaded yet?? I've tried the onload, and, like I said, the variable gets swapped appropriately but it doesn't show up on the page correctly. I figured there was just something I was missing to apply the new variable to the actual image url.

                    Thanks for trying to help. Don't spend your time on it. I'll figure it out (I think).

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      Originally posted by UberGirl
                      I thought you said I shouldn't do it onload because the page hasn't loaded yet??
                      “during page load” (the code executed by the parser) and “onload” (the event taking place immediately after the html parser has finished) are two very different things, though they seem to be similar. when only describing something, great care is to be taken because the other one (meaning me) has only this bit of information. (e.g. I have no idea, what the rest of your code is, so I have to guess from your desciption)

                      Originally posted by UberGirl
                      II figured there was just something I was missing to apply the new variable to the actual image url.
                      a common mistake to do…

                      Originally posted by UberGirl
                      Thanks for trying to help. Don't spend your time on it. I'll figure it out (I think).
                      I can’t approve of that attitude. I’m proceeding!

                      Comment

                      • UberGirl
                        New Member
                        • Aug 2009
                        • 9

                        #12
                        Okay - well thanks! So, here's what's not working...


                        This function is being called onload:

                        Code:
                        function ToggleDivImage() { 
                        var boxes = document.getElementsByTagName("img"); 
                        for(var i=0;i<boxes.length;i++)  
                        if(boxes[i].className.match("ti")) toggleimage(boxes[i]); 
                        }

                        Which calls this one:

                        Code:
                        function toggleimage(elem) { 
                        image = elem.src; 
                        if(image=='collapse.gif') 
                        alert("Should replace image"); 
                        image = 'expand.gif'; 
                        alert(image); 
                        }
                        As a result, I'm getting alerts telling me that it found collapse.gif and changed the variable "image" to expand.gif.


                        So now how do I get that new variable applied to my images in this html code?

                        Code:
                        <img class="ti" src="images/collapse.gif" />

                        I'm guessing I need something to apply the new value of the variable to the src, but I don't know exactly how to do that.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          Originally posted by UberGirl
                          So now how do I get that new variable applied to my images in this html code?
                          simply by redefining the src attribute.

                          Code:
                          function toggleimage(elem) 
                          {
                          // this way you cannot mix up = and == 
                              if ('collapse.gif' == elem.src) 
                                  var image = 'expand.gif'; 
                          // to make it toggle
                              else if ('expand.gif' == elem.src)
                                  var image = 'collapse.gif':
                          // change the image
                              [U]elem.src = image;[/U]
                          }
                          from here it’s not far from using the this keyword…

                          NOTE:
                          Code:
                          function toggleimage(elem) { 
                          image = elem.src; 
                          if(image=='collapse.gif') 
                          alert("Should replace image"); // end of [I]if ()[/I]
                          image = 'expand.gif'; // <--- this is executed regardless of the if condition
                          alert(image); 
                          }

                          Comment

                          • UberGirl
                            New Member
                            • Aug 2009
                            • 9

                            #14
                            Dormilich, you rock!

                            Thanks a lot - it's working great now. The page is working with or without scripts enabled. I knew it was something simple that I wasn't doing (or doing wrong). I didn't use the else if toggle, as it wasn't needed.

                            Your help is MUCH appreciated!

                            Thanks again!
                            UberGirl

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              Originally posted by UberGirl
                              I didn't use the else if toggle, as it wasn't needed.
                              then you should rename it, because it does not toggle the image (the next one reading your code may get confused by this)

                              Originally posted by UberGirl
                              Code:
                              if(boxes[i].className.match("ti")) toggleimage(boxes[i]);
                              why not using
                              Code:
                              var boxes = document.getElementsByClassName("ti");
                              ?

                              Comment

                              Working...