Script to show/hide DIVs (x-browser), syntax error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theory
    New Member
    • Jul 2010
    • 3

    Script to show/hide DIVs (x-browser), syntax error?

    Having an odd problem with this script- it's supposed to show/hid div's based on the option selected in a selection box. It's supposed to change methodology based on common browsers. The hardest part of my needs is that I explicitly need to support IE6, IE7, IE8, FF2, and FF3. Other browsers are optional, but that IE6 throws a monkeywrench in things.
    So I came up with this script, and now it supports FF2-3, but IE is catching an error, "Expected ')'" on lines (12 & 24 here).
    I tried adding a '); to line 24 (you can see its difference from line 12) but it led me through several other errors, and still different work. I'm not really a javascripter, I'm PHP and Ruby- I get the general idea but I don't know enough about javascript to debug this, unfortunately.

    Code:
    var browserType;
    if (document.layers) {browserType = "nn4"}
    if (document.all) {browserType = "ie"}
    if (window.navigator.userAgent.toLowerCase().match("gecko")) {
     browserType= "gecko"
    }
    function hide(arg) {
      if (browserType == "gecko" )
    	 document.poppedLayer =
    		 eval('document.getElementById("' + arg + '")');
      else if (browserType == "ie")
    	 document.poppedLayer = eval('document.getElementById("' + arg + '"');
      else
    	 document.poppedLayer =
    		 eval('document.layers["' + arg + '"]');
      document.poppedLayer.style.display = "none";
    }
    function show(arg) {
      if (browserType == "gecko" )
    	 document.poppedLayer =
    		 eval('document.getElementById("' + arg + '")');
      else if (browserType == "ie")
    	 document.poppedLayer =
    		eval('document.getElementById("' + arg + '")'); 
      else
    	 document.poppedLayer =
    		 eval('document.layers["' + arg + '"]');
      document.poppedLayer.style.display = "inline";
    }
    function update() {
    	if(getSelectedValue() == 0){
    		show("numeric_minimum");
    		show("numeric_maximum");
    		show("number_desirable");
    		hide("tf_desirable");
    		hide("multiple_answer_count");
    		hide("multiple_answer_captions");
    		hide("multiple_answer_efficiencies");
    	}
    	if(getSelectedValue() == 1){
    		hide("numeric_minimum");
    		hide("numeric_maximum");
    		hide("number_desirable");
    		show("tf_desirable");
    		hide("multiple_answer_count");
    		hide("multiple_answer_captions");
    		hide("multiple_answer_efficiencies");
    	}
    	if(getSelectedValue() == 2){
    		hide("numeric_minimum");
    		hide("numeric_maximum");
    		hide("number_desirable");
    		show("tf_desirable");
    		hide("multiple_answer_count");
    		hide("multiple_answer_captions");
    		hide("multiple_answer_efficiencies");
    	}
    	if(getSelectedValue() == 3){
    		hide("numeric_minimum");
    		hide("numeric_maximum");
    		hide("number_desirable");
    		hide("tf_desirable");
    		show("multiple_answer_count");
    		show("multiple_answer_captions");
    		show("multiple_answer_efficiencies");
    	}
    	if(getSelectedValue() == 4){
    		hide("numeric_minimum");
    		hide("numeric_maximum");
    		hide("number_desirable");
    		hide("tf_desirable");
    		show("multiple_answer_count");
    		show("multiple_answer_captions");
    		show("multiple_answer_efficiencies");
    	}
    	if(getSelectedValue() == 5){
    		hide("numeric_minimum");
    		hide("numeric_maximum");
    		hide("number_desirable");
    		hide("tf_desirable");
    		hide("multiple_answer_count");
    		hide("multiple_answer_captions");
    		hide("multiple_answer_efficiencies");
    	}
    }
    function getSelectedValue() {
    	var index = document.getElementById("selector").selectedIndex;
    	return index;
    }

    Thanks in advance to anyone that can help me with this, it is for a project on a deadline by tommorow at 1pm; I know it's unrealistic to hope for anything, all I'm saying is that it's important to me. Or, as an alternative, if someone is familiar with another existing script that works for at least the browsers I mentioned above, that would work too.

    Thanks guys! If you need more info just ask- I figured the script kind of speaks for itself (its a small addition to a very large CMS, but it's the only part having any trouble).


    [Edit: Does not support IE6- I had a third party test this, and after testing myself, it appears his results were incorrect- it does not work in IE, at all, ever.]
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Can i know why you are using "document.poppe dLayer". Is there anything to do with document there.

    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      general remark:

      do not check for specific browsers. there are so many browsers and browser versions out, that even if you find the correct browser (given that the browser doesn’t send a fake UA string) you can’t rely that the code works.

      nowadays you do "feature detection", that is, you test whether a specific method exists before you use it.
      Code:
      var x = "generic string";
      if (x.rtrim) {
          x = x.rtrim();
      }
      else if (x.replace) {
          x = x.replace(/\s+$/, "");
      }
      else {
          // some really basic trimming method
      }
      besides, document.layers and document.all is completely outdated, since there are better methods available.

      Comment

      • theory
        New Member
        • Jul 2010
        • 3

        #4
        show / hide DIV

        I was using someone else's script, because my knowledge of JavaScript is too limited to come up with a cross browser solution myself. You can find the original script here:
        Real's HowTo : Useful code snippets for Java, JS, PB and more


        I'm looking into the feature detection, to see if I can figure this out.

        For a quick fix, does anyone know of a good show/hid DIV script that supports IE7+, IE6, and FF2+? Other browsers would be nice for future support, but these are our only priorities because so far they are the only browsers our clients are using.

        What would be an example of a better method to use than document.layers / document.all?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          document.getEle mentById()

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            Hi,
            You can achieve that easily. I am giving u a sample

            HTML Code:
            Code:
                <div id="myDiv">
                   your div content...
                </div>
            JS Code: To Hide
            Code:
            document.getElementById('myDiv').style.display='none'
            JS Code: To Reshow the Element
            Code:
            document.getElementById('myDiv').style.display='block'
            Thanks and Regards
            Ramanan Kalirajan

            Comment

            • theory
              New Member
              • Jul 2010
              • 3

              #7
              I was able to resolve this issue using getElementById( ) and a solution quite similar to RamananKaliraja n's. Thank you everyone for your help- I turned it in a bit late but it would have been much later without you. Thanks again.

              Comment

              Working...