onclick problem

X

Xu, Qian

Hi All,

I wrote some code but it doesn't work.

------------------------------------------
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error
------------------------------------------

Browser says "MyClass" has no property. But if I replace
onclick="MyClass.btnClickHandler" with onclick="alert(0)", it works. How
to bind an event to MyClass.btnClickHander ?

BTW: If I bind this event using Prototype, it works.
 
T

Tom de Neef

Xu said:
------------------------------------------
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error
------------------------------------------

Browser says "MyClass" has no property. But if I replace
onclick="MyClass.btnClickHandler" with onclick="alert(0)", it works. How
to bind an event to MyClass.btnClickHander ?

A few changes help:
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body onload = MyClass.init()>
----------^ so that the DOM is complete
</html>

// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler()"
------------------------------------------------------------------^ brackets
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHandler : function() {
----------------^ spelling "l" missing
alert('Done');
}
}

// MyClass.init();
^ now in onload

Tom
 
X

Xu, Qian

Tom said:
// MyClass.init();
^ now in onload

Tom

Thanks Tom, but this does not help. I am more interested in Why?

One thing I do not get, when button is clicked successfully,
MyClass.btnClickHandler would be called. I called "alert(this.tagName)",
it is "BUTTON" not "MyClass". The "this" pointer should not be MyClass?
 
T

Tom de Neef

"Xu, Qian"wrote:
Thanks Tom, but this does not help. I am more interested in Why?

One thing I do not get, when button is clicked successfully,
MyClass.btnClickHandler would be called. I called "alert(this.tagName)",
it is "BUTTON" not "MyClass". The "this" pointer should not be MyClass?

I suggested three changes. I tested the code with these changes. It works
fine.
Tom
 
Á

Álvaro G. Vicario

Xu, Qian escribió:
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error

I get this error:

bd has no properties
bd.innerHTML = injectCode + bd.innerHTML;

You call init() from <head> tag, with no timer or event handling. At
that point, it's likely that the isn't any <body> yet.

You should launch init() when page has loaded or when DOM is ready:

window.onload=function(){
MyClass.init();
};

The above lines are only a quick example, it's not the recommended way
to do it because you'll overwrite any other onload actions you may have
defined. If you get the addEvent() function from:

http://dean.edwards.name/weblog/2005/10/add-event2/

.... you could change this into:

addEvent(window, "load", function(){
MyClass.init();
});

You can also Google for "onDomReady" or "domReady".
 
X

Xu, Qian

Tom said:
"Xu, Qian"wrote:

I suggested three changes. I tested the code with these changes. It works
fine.
Tom

Thanks, it was my fault.
1. It works when I use attachEventListner (for Firefox)
2. I should write

this.btnClickHander = function() {
// accessing methods or properties of another class works now ;)
}
 
X

Xu, Qian

Álvaro G. Vicario said:
Xu, Qian escribió:

I get this error:

bd has no properties
bd.innerHTML = injectCode + bd.innerHTML;

You call init() from <head> tag, with no timer or event handling. At
that point, it's likely that the isn't any <body> yet.

You should launch init() when page has loaded or when DOM is ready:

window.onload=function(){
MyClass.init();
};

The above lines are only a quick example, it's not the recommended way
to do it because you'll overwrite any other onload actions you may have
defined. If you get the addEvent() function from:

http://dean.edwards.name/weblog/2005/10/add-event2/

... you could change this into:

addEvent(window, "load", function(){
MyClass.init();
});

You can also Google for "onDomReady" or "domReady".

Thanks for the tip. Yes, I should wait until the DOM tree has been
loaded completely. Currently I use attachEventListner for Firefox and it
works.
 
T

Tom de Neef

this.btnClickHander = function() {

I think you still miss an "l": Handler instead of Hander
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top