What I want to do is to separate all my JavaScript from the page, so it works for all with disabled JavaScript, but also be able to compile it using closure compiler's advanced compiling (http://closure-compiler.appspot.com/home).
Here is how I started:
From...
<a href="javascript:doMultiple('download', 'release');">Create archive</a>
to...
<a class="javascript doMultiple download release" href="ca.php">CreateArchive</a>
Then I loop all objects to find what have the class javascript first, etc: (links is an array of the a tags)
for (i = 0; i < links.length; i = i + 1) {
if (links[i].className.substr(0, 10) === "javascript") {
jsArray = links[i].className.split(" ");
links[i].style.display = "inline";
if (links[i].addEventListener) {
links[i].addEventListener("click", window[jsArray[1]], false);
} else {
links[i].onclick = window[jsArray[1]];
}
links[i].href = "#";
}
}
With this method javascript compressors will remove the function doMultiple because it's unused because it dosnt know I have a class with that...
And my question is, how can I solve this, and if I can't, can I somehow get rid of the window[jsArray[1]] and do solve that in another way? Because I think that is not the best way to do it.
A class can also be:
<a class="javascript popUp register.php 350 300" href="register.php">Register</a>
So I want to use this method for adding all JavaScript dynamically.
Best regards,
Johan Svensson
classattribute is a bad idea, much worse than using an inlineonclick, though I'd agree that you shouldn't put JavaScript in thehrefattribute. If you want it to work with or without JavaScript enabled usehrefto specify non-JavaScript navigation, and anonclickto do something fancier.