Is your HTML DOCTYPE transitional? If not, maybe your code should look more like:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<body>
<input type='button' value='Show alert box' onclick='myFunction()' />
<script type='text/javascript'>
function myFunction(){
alert('I am an alert box!');
}
</script>
</body>
</html>
Of course, I rearranged it. Your code should work though. I personally would recommend that your code look more like:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<body>
<input type='button' value='Show alert box' id='btn' />
<script type='text/javascript' src='external.js'></script>
<script type='text/javascript'>
showAlert('btn');
</script>
</body>
</html>
external.js
will be cached. It can look something like:
function E(e){
return document.getElementById(e);
}
function showAlert(e){
E(e).onclick = function(){
alert('I am an alert box!');
}
}
The latter code is more reusable, and will give you an idea about jQuery's $()
. It's a way more sophisticated version of my E()
function.