Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script not working on NN6

    Here is a javascript which works in IE and NN4.7. But its not working in NN6. I wrote that else statement to handle NN6 with getElementById. Can some one check?

    <SCRIPT LANGUAGE="javascript">

    //global variables

    var layerRef="null",styleSwitch="null";
    var layerShowing="layer1";

    function init(){
    if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 6) {
    layerRef="document.layers";
    styleSwitch="";
    }else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >=6) {
    layerRef="document.getElementById()";
    styleSwitch=".style";

    }else {
    layerRef="document.all";
    styleSwitch=".style";
    }

    }
    function changeDisplay(choice){
    //don't do anything if no option was selected
    hideLayer(layerShowing);
    if (choice == "Compendex"){
    var layerToShow="layer1";
    }else if (choice == "All"){
    var layerToShow="layer2";
    }else if (choice == "Inspec"){
    var layerToShow="layer3";
    }else if (choice == "NTIS"){
    var layerToShow="layer4";
    }else if (choice == "Patents"){
    var layerToShow="layer5";
    }
    showLayer(layerToShow);
    layerShowing=layerToShow;
    }

    function showLayer(layerName){
    eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
    }
    function hideLayer(layerName){
    eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');
    }

    </script>

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Obviously I can't test, due to not having all your code, and this is a really bad way to do things, but I've made a couple of small changes.
    Code:
    <script type="text/javascript">
    	//global variables
    	
    	var layerRef = '';
    	var styleSwitch = '';
    	var layerShowing = 'layer1';
    	
    	function init()
    	{
    		if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 6)
    		{
    			layerRef = "document.layers";
    			styleSwitch = "";
    		}
    		else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >=6)
    		{
    			layerRef = "document.getElementById";
    			styleSwitch = ".style";
    		}
    		else
    		{
    			layerRef = "document.all";
    			styleSwitch = ".style";
    		}
    	}
    	
    	function changeDisplay(choice)
    	{
    		//don't do anything if no option was selected
    		hideLayer(layerShowing);
    		
    		switch(choice)
    		{
    			case 'Compendex':
    				var layerToShow = 'layer1';
    				break;
    			case 'All':
    				var layerToShow = 'layer2';
    				break;
    			case 'Inspec':
    				var layerToShow = 'layer3';
    				break;
    			case 'NTIS':
    				var layerToShow = 'layer4';
    				break;
    			case 'Patents':
    				var layerToShow = 'layer5';
    				break;
    		}
    		showLayer(layerToShow);
    		layerShowing = layerToShow;
    	}
    	
    	function showLayer(layerName)
    	{
    		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');
    	}
    	
    	function hideLayer(layerName)
    	{
    		eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="hidden"');
    	}
    </script>
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much hemebond for tidying up the code. So after your post is it ok to use that code or its still a bit bad code?

    Also my question was when I was chekcing for the different browsers, for Netscape6 I saw a code in the web like:

    document.getElementById("layer name").style.visibility="visible"

    But what do you pass for the "layer name" according to my code?

    Because if you see at the bottom of the code, we are using like
    eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility="visible"');

    This is valid for IE and NN4.X But for NN6.X what should be the value for "layer name" in document.getElementById("layer name").style.visibility="visible"?

    I think this is what causing it not to work in NN6.X

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    That is an old style of cross-browser scripting. Object detection is more appropriate in this case.
    Code:
    function getStyle(layerName){
      if (document.getElementById){
         return document.getElementById(layerName).style;
      }
      if (document.all){
         return document.all[layerName].style;
      }
      if (document.layers){
         return document.layers[layerName];
      }
    }
    
    function showLayer(layerName)
    {
      getStyle(layerName).visibility="visible";
    }
    
    function hideLayer(layerName)
    {
      getStyle(layerName).visibility="hidden";
    }
    I advise you not to use eval as it is inefficient. Almost always, it can be achieved by using square bracket notation. See my sig for more info on it.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much glenngv. I got it to work in all browsers

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    NS6 accept getElementById() method as well, so that, by short, a smart crossbrowser code, as glenngv briefly pointed ot, should not have more than

    if(document.getElementById)// ie5+,NS6+, Moz and all the modern compatible browsers

    else if(document.all)//IE4

    else if(document.layers)//NS4

    The problem is that when google, you may find a lot of old codes which have only IE4/NS4 or even worse, the sequence is not correct (getElementById() should be first, to avoid duplicate the actions) and, furthermore, I guess that else if should be the correct switcher as well to avoid that.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,968
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by Kor
    I guess that else if should be the correct switcher as well to avoid that.
    I didn't use that in my case because I have the return statement inside the if block.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    aha. yes, sorry, I have not noticed that... You're right
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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