Forum Moderators: open

Message Too Old, No Replies

How do I create a delay to new location onclick link script?

         

MarcMiller

6:30 am on Nov 13, 2006 (gmt 0)

10+ Year Member



Hi
I wish to create an onclick delay for my page links. I wish to do this because I have a site in which I would like to have all the links have a sound indication giving the user feedback that he made the click. I wrote a little code to test the concept. It worked well usually in Firefox however it failed to work in Internet Explorer. I believe the reason is that Internet Explorer moved to the new location before the sound had a chance to play. I believe this because in my little demonstration code when I do not link a link to a new location I hear the indication sound just fine. However when I link to a new location it appears I get to this new location before the sound has a chance to play. The press in sound is of short duration but is not the sound I would ultimately use is just a test sound. My little demonstration script and HTML code appears below a long with a link to my demonstration page. If it would be possible to get some help writing a script that would degrade gracefully if JavaScript was turned off I would be very appreciative. However I may be able to figure out the degrade gracefully part myself if I could get some suggestions on how I go about learning to write this script or just get it here. Tell me what you can. Here is the script, HTML, and link.

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>&nbsp;&nbsp;
<a href="under constructionYyyl.html">
link to under construction location
</a>
<br><br><br>&nbsp;&nbsp;
<a href="#">
link with out new location
</a>
<br><br><br>&nbsp;&nbsp;
<a href="under constructionYyyl.html">
link to under construction location
</a>
<br><br><br>&nbsp;&nbsp;
<a href="#">link with out new location
</a>
<br><br><br>&nbsp;&nbsp;
</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]

Fotiman

3:37 pm on Nov 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As for degrading gracefully, you're already well on your way. You've attached your event handler's in an unobtrusive way, so that's good.

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.

MarcMiller

7:32 am on Nov 14, 2006 (gmt 0)

10+ Year Member



Thank you a lot for that advice.
For the moment I am going to post , to a design forum, a Firefox only version of the site I wish to uses concept on just to see if the designers there think this concept is worthwhile pursuing. I may take the liberty of further asking about what was said above in this thread.
Thanks again
Marc