Forum Moderators: open

Message Too Old, No Replies

Can somebody tell me what's wrong with this attach sound to my anchors

         

MarcMiller

9:54 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



Hi
I am attempting to write a script that resides in a separate linked file as per the latest recommendations on keeping your scripts in a separate file they call the behavior layer. The script attaches sounds too in onclick event to all tags of type anchor (<a> tags.) That is if you can help me get it to work its will. When I write a script which uses IDs for the attachments it works fine see below.

HTML
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Attach sound per anchor ID</title>
<meta content="text/html; charset=us-ascii" http-equiv="Content-Type" />

<script src="attachSoundPerAnchorID.js" type="text/javascript"></script>
</head>
<body>
<embed id="thup" src="thup.wav" loop="false"
autostart="false" hidden="true" enablejavascript="true"></embed>
<p>&nbsp;&nbsp;
<a id="anchor1" href="#">one</a>
<br><br><br>&nbsp;&nbsp;
<a id="anchor2" href="#">two</a>
</p>
</body>
</html>

Javascript
function onClickSound(){
document.getElementById('thup').Play();
}
window.onload = function () {
document.getElementById("anchor1").onclick=onClickSound;
document.getElementById("anchor2").onclick=onClickSound;
}

However when I try to use an anchors collection it does not work see below.

HTML
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Attach sound on all anchors</title>
<meta content="text/html; charset=us-ascii" http-equiv="Content-Type" />

<script src="AttachSoundOnAllAnchors.js" type="text/javascript"></script>
</head>
<body>
<embed id="thup" src="thup.wav" loop="false"
autostart="false" hidden="true" enablejavascript="true"></embed>
<p>&nbsp;&nbsp;
<a href="#">one</a><br><br><br>&nbsp;&nbsp;
<a href="#">two</a>
</p>
</body>
</html>

JavaScript
var anchors;
function onClickSound(){
document.getElementById('thup').Play();
}

function allAanchor(){
for (var i=0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick=onClickSound();
}
}

window.onload = function () {
anchors = document.getElementsByTagName("a");
allAanchor();
}

The JavaScript and HTML code immediately above does not work. I put a "var anchor;" statement outside of all functions since including it inside the functions generated an anchors variable not defined error. But this only gets rid of the error it does not let the script work. So I would be most appreciative if someone here to tell me how I attach a sound to all anchor tags on a HTML page without explicitly giving each anchor an ID and then explicitly writing code to attach a sound to that ID.

Very sincerely
Marc

penders

12:04 am on Nov 7, 2006 (gmt 0)

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



Hi Marc,

In your first example, which you say works, you have:

document.getElementById("anchor1").onclick=onClickSound;

But in your second example, you have:

anchor.onclick=onClickSound();

I don't think you should have the brackets after the function name, when you are assigning the function itself. (Otherwise I guess you will end up assigning the output of the function instead?!)

Just a side issue... is there a reason why your 'anchors' variable needs to be global, can it not just be a local variable within the allAanchor() function?

MarcMiller

3:29 am on Nov 7, 2006 (gmt 0)

10+ Year Member



Thanks penders, your observation about calling the function with parentheses behind the function name was what was keeping it from working. As for that global variable thing it works whether you declarable global variable in line 1 of the code or not. As long as I create the anchor's collection without using the VAR keyword in front of that line in my onload function. I do not know if the onload event is somehow creating a global variable or not. I have not seen the syntax of evoking a function without parentheses documented and would be very interested if anyone could direct me to some reference, tutorial, or anything else that explains this to me for the reasons for this is a wonder to me. I just do it that way from copying. The script that works with no change to the HTML is posted below.

Your sincere thanks.
Marc


function onClickSound(){
document.getElementById('thup').Play();
}

function allAanchor(){
for (var i=0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick=onClickSound;
}
}

window.onload = function () {
anchors = document.getElementsByTagName("a");
allAanchor();
}

penders

10:29 pm on Nov 7, 2006 (gmt 0)

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



...As for that global variable thing it works whether you declarable global variable in line 1 of the code or not. As long as I create the anchor's collection without using the VAR keyword in front of that line in my onload function. I do not know if the onload event is somehow creating a global variable or not.

That is because when you don't use the VAR keyword, the variable is implicitly defined as a global. Which could cause problems in other situations, so always best to explicitly declare your variables with 'var'. And to always use local variables wherever possible.

So, your code could be rewritten:

function allAanchor() {  
var anchors = document.getElementsByTagName("a");
for (var i=0; i < anchors.length; i++) {
anchors[i].onclick=onClickSound;
}
}

window.onload = function () {
allAanchor();
}

I have not seen the syntax of evoking a function without parentheses documented...

In your case, without parentheses, you are not 'evoking' the function. You are actually assigning the entire function to another variable. The function itself is not executed (evoked) until your onclick event fires.

MarcMiller

11:12 am on Nov 8, 2006 (gmt 0)

10+ Year Member



In your case, without parentheses, you are not 'evoking' the function. You are actually assigning the entire function to another variable. The function itself is not executed (evoked) until your onclick event fires.

Then assigning the function to another variable is something very different then passing a variable to an algebraic formula or function?