I have a web application where a lot of inline javascript is used. For example:
<input type="button" id="btn1" onclick="return test();">
I want to separate javascript code from the UI by placing it in a separate file or inside the script tag. Including javascript event attachment. For example instead of the above I'd have:
<input type="button" id="btn1">
and in the script section I'd us:
<script type="text/javascript>
btn1.onclick = function() {
test();
}
</script>
or
<script type="text/javascript>
if (btn1.addEventListener){
btn1.addEventListener('click', test, false);
}else if (btn1.attachEvent){
btn1.attachEvent('onclick', test );
}
</script>
Why do I want to do that? I like separating my UI from the business logic and database, that's my general approach. Also it is better from performance standpoint, as external javascript can be easier cached by browsers. Another reason - it is more secure. My question is whether this is worth the hassle of changing the application, removing all the inline javascript?
I'd appreciate if someone gave me some pointers, some links to articles - I have been searching the web and found some conflicting information. Would appreciate to hear some opinions