I'm trying to get this code to work. Pretty much if the URL has #leaf in it then the tag updates to display image2.png. I'm not sure what I'm doing wrong.
<script type="text/javascript">
$("#image").show("fast") {
// Which anchor is being used?
switch(window.location.hash) {
case "#leaf":
window.location.href = image1.png
break;
case "#carrot":
window.location.href = image2.png
break;
}
});
</script>
<a href="#leaf">Leaf</a>
<a href="#carrot">Carrot</a>
<img id="image" />
PS: I'm open to better ways of achieving this. Just tried the following but still doesn't work.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
$("#image").show("fast", function() {
// Which anchor is being used?
switch(window.location.hash) {
case "#leaf":
$(this).attr('src', 'image1.png');
break;
case "#carrot":
$(this).attr('src', 'image2.png');
break;
}
});
</script>
</head>
<body>
<a href="#leaf">Leaf</a>
<a href="#carrot">Carrot</a>
<img id="image" />
</body>
</html>