HTML / Javascript callback not executing
The HTML form sh开发者_如何学JAVAows but the javascript callback is not executing. (and how do I combine into one file?)
thx
<html>
<head>
<title> Mouse click test</title>
</head>
<body>
<h1>Mouse Click Test</h1>
<p>Click the mouse on the test link below. A message below will indicate which button was clicked.</p>
<h2><a href = "#" id = "testlink">Test Link</a></h2>
<form name = "form1">
<textarea rows = "10" cols = "70" name = "info"></textarea>
</form>
<script type = "text/javascript" scr = "click.js">
</script>
</body>
</html>
function mousestatus(e){
document.write("hello");
if (!e) e = window.event;
btn = e.button;
whichone = (btn < 2) ? "Left" : "Right";
message = e.type + " : " + whichone + "\n";
document.form1.info.value += message;
}
obj = document.getElementById("testlink");
obj.onmousedown = mousestatus;
obj.onmouseup = mousestatus;
obj.onclick = mousestatus;
obj.ondblclick = mousestatus;
The attribute for the <script>
tag is src
, not scr
. This:
<script type = "text/javascript" scr = "click.js">
Should be:
<script type="text/javascript" src="click.js">
And get rid of the document.write
call, document.write
will:
Writing to a document that has already loaded without calling
document.open()
will automatically perform adocument.open
call.
And document.open
:
If a document exists in the target, this method clears it.
So calling document.write
anywhere except inside an inline <script>
will erase everything before inserting your new stuff; about the only time you should use document.write
is in things like this:
<p>Where is <script type="text/javascript">document.write('pancakes')</script> house?</p>
You'd usually use console.log('...')
or console.debug('...')
for quick debug messages (assuming that you're using a browser with a console and you have the console open).
Once you take care of those issues, you should have something that does more interesting things than throw errors (or nothing at all): http://jsfiddle.net/ambiguous/5kHVy/
You might want to read up the newer event handling interfaces (such as addEventListener
).
Shouldn't it be src="click.js"?
<script type = "text/javascript" scr = "click.js">
^^^
精彩评论