Button rollover and external script issue

Discussion in 'JavaScript' started by Mentalhead, Dec 23, 2010.

  1. #1
    I'm new to JavaScript, and I need to get this done for a school project, so can someone explain me what am I doing wrong here?
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    		<title>Untitled Document</title>
    		<script type="text/javascript" src="test.js"></script>
    		<style>
    			@import "stil.css";
    		</style>
    	</head>
    	<body >
    		<a href="http://www.w3schools.com" onmouseover="slika()"><img src="http://t3.gstatic.com/images?q=tbn:ANd9GcQI6lrcdFX8FRV9NqojvemGnRb-KD-_UuHd-nlqFW4kKzufIHRL" id="slika"></a>
    	</body>
    </html>
    
    Code (markup):
    
    function slika(){
    	
    	document.getElementById("slika").src="http://collections.vam.ac.uk/images/template/ajax-loader.gif";
    }
    function funk(){
    	alert("BRE?!");
    }
    
    Code (markup):
    For some strange reason this doesn't work, but if I don't use the function, like this:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    		<title>Untitled Document</title>
    		<script type="text/javascript" src="test.js"></script>
    		<style>
    			@import "stil.css";
    		</style>
    	</head>
    	<body >
    		<a href="http://www.w3schools.com" onmouseover="document.getElementById('slika').src='http://collections.vam.ac.uk/images/template/ajax-loader.gif';"><img src="http://t3.gstatic.com/images?q=tbn:ANd9GcQI6lrcdFX8FRV9NqojvemGnRb-KD-_UuHd-nlqFW4kKzufIHRL" id="slika"></a>
    	</body>
    </html>
    
    Code (markup):
    everything works like a charm.
    So can someone explain me what am I doing wrong here?
    Function funk() works without any problems when it's called, so I'm guessing that something is missing in my other function.
     
    Mentalhead, Dec 23, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    The function name had the same name as the id of the element , try unique names.
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    		<title>Untitled Document</title>
            
            <script type="text/javascript">
            function slik(){
                document.getElementById("slika").src="http://collections.vam.ac.uk/images/template/ajax-loader.gif";
            }
            </script>
    	</head>
    	<body >
    		<a href="http://www.w3schools.com" onmouseover="slik()">
                <img src="http://t3.gstatic.com/images?q=tbn:ANd9GcQI6lrcdFX8FRV9NqojvemGnRb-KD-_UuHd-nlqFW4kKzufIHRL" id="slika" />
            </a>
    	</body>
    </html>
    
    HTML:
     
    tvoodoo, Dec 23, 2010 IP
  3. Mentalhead

    Mentalhead Active Member

    Messages:
    1,344
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Tried it, but it doesn't work.
     
    Mentalhead, Dec 23, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Works fine for me.... Clear your cache.
     
    tvoodoo, Dec 23, 2010 IP
  5. Mentalhead

    Mentalhead Active Member

    Messages:
    1,344
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #5
    It works now, although I didn't clear my cache.
    Thanks :)
     
    Mentalhead, Dec 24, 2010 IP