开发者

JavaScript: trouble assigning event handlers

I'm not sure what I'm doing wrong here:

index.html

<?xml version="1.0" ?开发者_运维百科>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "XHTML1-s.dtd" >
<html xmlns="http://www.w3.org/TR/1999/REC-html-in-xml" xml:lang="en" lang="en" >

<head>
    <script type="text/javascript" src="scripts/eventInit.js"></script>
</head>
<body>
<p id="javascriptWarning">This page will not work with JavaScript disabled.</p>
</body>
</html>

eventInit.js

window.onload = function () {
    alert("check"); // works
    var jsWarning = document.getElementById("javascriptWarning");
    jsWarning.onclick = function () {
        alert("hi"); // works
    };
    jsWarning.onload = function () {
        alert("loaded"); // fails
    };
}

And yet, nothing happens. What am I doing wrong? I've tried other events, like onmouseover and onload.

I'm doing this in Visual Studio, and intellisense isn't giving me options for setting any event handlers. Is that because I'm doing this wrong?

I have confirmed that JS is working on my setup; just putting alert("hi") in a script and including it does work.

It might be important to note that I'm doing this in JScript, since I'm using Visual Studio 2010, so perhaps event handling is different?

Updated to remove '-' from the ID name, but it still doesn't work.

Updated added the window.onload block. Now onclick works, but onload doesn't.


You are trying to set a load event on a paragraph. Only objects which load external data (window, frame, iframe, img, script, etc) have a load event.

Some JS libraries implement an available event (such as YUI) — but you know the paragraph is available, since you're setting an event on it, and you couldn't do that if it was unavailable.


maybe you forgot to have the code block inside a

window.onload = function() {
          // btn click code here 
  }


  1. You have to wait for the document to be parsed before you can go looking for elements by "id" value. Put your event handling setup into an "onload" function on the window object.
  2. The browser won't fire an "onload" event on your <p> tag. You won't need that anyway if you do your work in the "onload" handler for the window as a whole.
  3. [soapbox] Use a framework.


The script is executed before the desired element exists. Additionally, I don't think, p has an onload-Event. Windows, frames and images, yes, but paragraphs?

You should use <body onload="init();"> or window.onload=function(){ … } or a library function, if you use a library. Example:

index.html

<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "XHTML1-s.dtd" >
<html xmlns="http://www.w3.org/TR/1999/REC-html-in-xml" xml:lang="en" lang="en" >
<head>
<script type="text/javascript" src="scripts/eventInit.js"></script>
</head>
<body>
<p id="javascriptWarning">This page will not work with JavaScript disabled.</p>
</body>
</html>

scripts/eventInit.js

window.onload=function(){
  alert('JS is working!');}

Edit: Okay, I am very sure, p makes no use of an onload event handler. And it's no wonder, you don't need it. If you want to execute JS code just after the paragraph is finished, do this:

<p>
<!-- stuff -->
</p>
<script type="text/javascript">
/* stuff */
</script>


Instead of this:

jsWarning.onload = function () {
        alert("loaded"); // fails
    };

try this

if(jsWarning) alert("loaded");

I think someone above mentioned checking for the existence of the element. At this stage the element should be present but it does no harms to check for it.


I think you have to make sure your JavaScript is binding.

Is your javascript before or after your paragraph element, for some reason my brain is aiming towards that.

I would look into using something like jQuery, it will help.

using jQuery your code would be (with the relevant jQuery files included of course):

$(document).ready(function()
{
    $("#javascript-warning").click(function(){
        alert("HELLO");
    });
});


I don't think hyphens are valid in class names when used in conjunction with JavaScript. Try an underscore instead.


onload is a window event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜