javascript gurus

Discussion in 'OT Technology' started by fatmoocow, Jan 11, 2004.

  1. fatmoocow

    fatmoocow bored OT Supporter

    Joined:
    Aug 27, 2002
    Messages:
    18,329
    Likes Received:
    307
    Location:
    the intarweb
    ok, I suck at javascript. I'm making a navigation bar basically all it does is change images based on what page you are on and if you are mousing over an image. So when the page loads the current page's link/button should be arrowed. Then, when you mouse over other links the images of those change to the arrowed version. This all works fine except it doesn't happen when the page loads. It just loads the default no arrows image for all the buttons, then if you mouse over it changes properly.

    How do I get this to work on load

    Here's what I've got simplified for easier reading

    Code:
    
    <body  OnLoad="javascript: setFocus(); [b]initialize();[/b]">
    
    <SCRIPT LANGUAGE="JavaScript">
    
    <!-- 
    
    
    image1 = new Image();
    image1.src = "images/top_nav/projectsoff.gif";
    
    image2 = new Image();
    image2.src = "images/top_nav/templatesoff.gif";
    
    image3 = new Image();
    image3.src = "images/top_nav/solutionsoff.gif";
    
    image4 = new Image();
    image4.src = "images/top_nav/criteriaoff.gif";
    
    image6 = new Image();
    image6.src = "images/top_nav/resultsoff.gif";
    
    
    // -->
    </script>
    
    
    
    <SCRIPT LANGUAGE="JavaScript">
    
    <!-- 
    
    function initialize() {
    
    	 	if (location.href == 'http://project_list.php' || location.href == 'http://project_nav.php'  ||  location.href == 'http://situation.php'  ){
    
    			image1.src = 'images/top_nav/projects.gif';
    		}
    		if (location.href == 'http://criteria_add.php' || location.href == 'http://criteria_weigh.php' || location.href == 'http://criteria_results.php'  ){
    
    			image4.src = 'images/top_nav/criteria.gif';
    		}
    		if (location.href == 'http://option_add.php' || location.href == 'http://option_weigh.php'){
    
    			image3.src = 'images/top_nav/solutions.gif';
    		}
    		if (location.href == 'http://results.php'){
    
    			image6.src = 'images/top_nav/results.gif';
    		}
    		if (location.href == 'http://template_nav.php' || location.href == 'http://template_criteria_add.php' || location.href == 'http://template_criteria_weigh.php' || location.href == 'http://template_results.php'){
    
    			image2.src = 'images/top_nav/templates.gif';
    		}
    
    
    }
    
    // -->
    </script>
    
    

    the initialize code is reused with onmouseout for each button and works just fine :dunno: it just doesn't work in the function. Does the function have to be before the call in the body tag? The way I'm doing this is via php with multiple parts being put together depending on the page so it's difficult to call before.
     
  2. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    The JavaScript you have needs to come before the body tag. Usually this flavor of JavaScript would come in the header tags.

    Also, check your JavaScript errors. If you don't have super powers and can interpret the cryptic IE JavaScript errors, I'd recommend using Mozilla and enter "javascript:" in the URL and it will show you the JavaScript errors you have plus details and line numbers.
     
  3. fatmoocow

    fatmoocow bored OT Supporter

    Joined:
    Aug 27, 2002
    Messages:
    18,329
    Likes Received:
    307
    Location:
    the intarweb
    I'm not getting any errors in the current flavor, I'll try moving the function up.

    also when I'm loading the images it looks like this

    <a><img src="pictureoff.gif"> name="image1"></a> I think this could be the problem because I'm resetting the source. Is there any way to say like <img src="image1.src"> and have it load the correct one from the start, I tried this but it didn't work.
     
  4. fatmoocow

    fatmoocow bored OT Supporter

    Joined:
    Aug 27, 2002
    Messages:
    18,329
    Likes Received:
    307
    Location:
    the intarweb
    moving it didn't work :/
     
  5. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    This is very invalid HTML: <img src="pictureoff.gif"> name="image1">

    You need to get that '>' out of there.
     
  6. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    What browser are you using to test your code with?
     
  7. fatmoocow

    fatmoocow bored OT Supporter

    Joined:
    Aug 27, 2002
    Messages:
    18,329
    Likes Received:
    307
    Location:
    the intarweb
    sorry I just typed that really quick the actual version has the full address and no extra ">"

    I'm just using IE
     

Share This Page