Forum Moderators: open
JavaScript
function eventSound() {
var thissound=document.getElementById("sound1");
thissound.Play();
}
function allAanchor(){
var anchors = document.getElementsByTagName("a");
for (var i=0; i < anchors.length; i++)
anchors[i].onclick=eventSound;
}window.onload = function () {
allAanchor();
}
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta content="text/html; charset=us-ascii"
http-equiv="Content-Type" />
<title>yyyl</title>
<script src="yyyl.js" type="text/javascript"></script>
</head>
<body>
<embed src="success.wav" autostart=false
width=0 height=0 id="sound1" enablejavascript="true">
<p>
<a href="under constructionYyyl.html">
link to under construction location
</a>
<br><br><br>
<a href="#">
link with out new location
</a>
<br><br><br>
<a href="under constructionYyyl.html">
link to under construction location
</a>
<br><br><br>
<a href="#">link with out new location
</a>
<br><br><br>
</p>
</body>
</html> [edited by: encyclo at 11:34 am (utc) on Nov. 13, 2006]
[edit reason] No URLs please, see TOS [webmasterworld.com] [/edit]
As for your problem, here are some things to consider:
1. When you attach the onclick event handler to your links, you should probably only attach to <a> elements that have an href attribute. Otherwise, named links could also get the handler, which you probably don't want. For example: <a name="foo">Foo</a>. So after you do getElementsByTagName, check each of those nodes for the existence of an href attribute before you attach the handler.
2. Your handler should:
a. Stop the current click event from propagating so the link isn't followed immediately.
b. Use setTimeout to schedule when to change the current location. Get the location value from the href attribute of the node.
c. Play the audio clip.
For stuff like this, I prefer to use the Yahoo UI Library [developer.yahoo.com]. There's a great Event Utility with that library that makes it easy to work with events in a way that works with all browsers.
Hope that helps.