I’ve been trying to write a function that makes all links execute a function on click, but I have NO idea why this isn’t working:
function interceptLinks() {
var ls = document.links, numLinks = ls.length
for (i=0; i<numLinks; i++) {
if ls[i].onclick {
//Don't do anything, cause there's already code there!
}
else {
ls[i].onclick = "reloadInfo();";
}
}
}
It just throws a “interceptLinks is not defined” error when I run the function.
You’re missing the parentheses around your
ifcondition:That’s all. The error “interceptLinks is not defined” that you get when trying to run the function shouldn’t be the first error you see. When you include/compile the function in the first place you’ll get a syntax error (“unexpected identifier” or something of the sort). That’s the error you need to resolve; don’t worry about the next error until it’s dealt with.