Let say I have a page that has the following html:
<input type="button" id="clickme" value="Click Me" />
On the same page, I have the following script:
<script type="text/javascript">
$('#clickme').click(function(){
console.log("clicked");
});
</script>
Is it fine for the above to be inline on the page or is it better to have the console.log(...) in a libray, so it would be something like:
<script type="text/javascript src="external.js"></script>
<script type="text/javascript">
$('#clickme').click(function(){
LogToConsole("clicked");
});
</script>
To take it to the next level, is it even better to put the whole click function in an external library, so the code eventually becomes something like:'
<script type="text/javascript src="external.js"></script>
<script type="text/javascript">
ClickMe("clicked");
</script>
The above still contains inline javascript or is my understanding of inline javascript incorrect because it seems that it is impossible to avoid?
<button onclick="someFun(this)" ...>. It is avoided by adding script dynamically as you're doing, which is usually done in the head or just before the closing body tag.