User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » JS Question (calling a function from prototype) Page [1]  
BigMan157
no u
103352 Posts
user info
edit post

ok, so i have this function called doThis()

i'm trying to attach it to an onclick event from within an instantiation of a prototype class and keep getting a "not defined" error or an "uncaught exception" error when i try to alert() instead.

something like:

var iThing = document.createElement('div');
iThing.id = "iThing";
iThing.onclick = function(){doThis();};


also tried:
var iThing = document.createElement('div');
iThing.id = "iThing";
iThing.onclick = "doThis();"
var onClickHandler = new Function(iThing.onclick);
if (iThing.addEventListener) {
iThing.addEventListener('click', onClickHandler, false );
} else if (iThing.attachEvent) {
iThing.attachEvent('onclick', onClickHandler);
} else {
iThing.onclick = function(){doThis();};
}


anyone have any idea what i should add or do? Everything i can think of to search for on the Goog hasn't helped at all

7/31/2007 8:59:21 AM

DirtyGreek
All American
29309 Posts
user info
edit post

for the uncaught exception, attempt to catch it using a try/catch

http://www.quackit.com/javascript/tutorial/javascript_try_catch.cfm

and install firebug

http://addons.mozilla.org/firefox/addon/1843

it'll help you get the JS right

7/31/2007 10:26:06 AM

BigMan157
no u
103352 Posts
user info
edit post

yeah that doesn't really help

7/31/2007 1:50:16 PM

BigMan157
no u
103352 Posts
user info
edit post

i guess what i'm asking is how do I reference a function declared outside of a class

function doThis() {alert('hi');}

var AnObject = function (someVar)
{
this.Thing = someVar;
}

AnObject.prototype.annoy = function ()
{
doThis();
}

whatev = new AnObject(thingie);
whatev.annoy();


[Edited on July 31, 2007 at 4:16 PM. Reason : this would cause a "doThis not defined" error message]

7/31/2007 4:15:58 PM

scanZero
Veteran
265 Posts
user info
edit post

I was bored and I threw your code into a file and it worked in firefox 2.0. I did have to change the assignment of whatev.

<html>
<head>
<script language="javascript">
function doThis() {alert('hi');}

var AnObject = function (someVar)
{
this.Thing = someVar;
}

AnObject.prototype.annoy = function ()
{
doThis();
}

whatev = new AnObject("test");
whatev.annoy();

</script>
</head>
<body>
<a href="#" onclick="whatev.annoy();">test</a>
</body>
</html>

7/31/2007 10:12:19 PM

BigMan157
no u
103352 Posts
user info
edit post

i gave up, i think somewhere in all of my code i lost scope somewhere

8/1/2007 12:19:25 PM

 Message Boards » Tech Talk » JS Question (calling a function from prototype) Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.