Click to See Complete Forum and Search --> : javascript: constructor in event handler


cmiskow
October 13th, 2005, 04:25 PM
I have a javascript class and a function similar to these:

function accountEntry(number, type)
{
this.number = number;
this.type = type;
}

function processAcct(account)
{ // account is an accountEntry instance
alert(account.number);
}


I am trying to use these from an event handler, like this:

<input type='button' onclick='processAcct(new accountEntry('11111', 'Visa'));' />


This is giving me some trouble because the "this" keyword is global and therefore refers to the event rather than the object I am trying to construct. Is there a way around this problem?

PeejAvery
October 13th, 2005, 05:08 PM
It is hard to see what you are trying to accomplish. Can you post more code?

A few things to note...

This is a call to a function within a functions parameters. This can't be done. Also, try to stay away from spaces when loading parameters in JavaScript.
<input type='button' onclick='processAcct(new accountEntry('11111', 'Visa'));' />
The JavaScript call to "this" refers specifically to the item being addressed. Here is an example of how it would work properly. "This" refers back to its object.
<input type="text" onclick="this.blur()">

cmiskow
October 13th, 2005, 05:48 PM
I'm sorry, the example I posted actually did work, I simplified it too much to where the problem no longer existed :(

In my case I have a window which opens a popup. The popup has a button which should populate the form on its opener. The opener page already has a javascript function which can be used to populate itself. Some of the parameters to this function are custom defined objects such as accountEntry.

So, I was trying to call the function on the opener page like this:
Opener window:

function accountEntry(number, type)
{
this.number = number;
this.type = type;
}
function loadForm(param1, param2, accountEntryParam)
{
// load the form using the parameters given.
}


Popup window:

<input type='button' onclick="window.opener.loadForm('x', 'y', new window.opener.accountEntry('11111', 'Visa'));" />


For some reason the "new window.opener.accountEntry(...)" part doesn't work (in IE 6 at least), while in a different place I can call it from an iframe like "new parent.accountEntry(...)" with no problem. Through debugging I found that it is recognizing the function and the class on the opener page, but it simply doesn't like creating an instance of the class. I was able to solve this problem by redefining the accountEntry class in the popup page and just using "new accountEntry(...)" instead.

I am curious about what you had to say, peejavery:
"This is a call to a function within a functions parameters. This can't be done."

Seems like a pretty common thing to do... e.g. parseInt(getRandomIntString()). Maybe I don't understand what you mean?

"try to stay away from spaces when loading parameters in JavaScript."

Is there a reason? I've been using javascript for quite a while and I've never had a problem with this, plus I find it easier to read.

PeejAvery
October 13th, 2005, 06:05 PM
I am curious about what you had to say, peejavery:
"This is a call to a function within a functions parameters. This can't be done."

Seems like a pretty common thing to do... e.g. parseInt(getRandomIntString()). Maybe I don't understand what you mean?
I didn't mean in general. I mistook the return of "account". I glanced over it and didn't think it would return because I didn't see the parameter in the function call. "processAcct(account)"

"try to stay away from spaces when loading parameters in JavaScript."

Is there a reason? I've been using javascript for quite a while and I've never had a problem with this, plus I find it easier to read.
I now work with JavaScript and PHP a lot. I am not sure why I encounter this, maybe it is a Firefox thing, either way I don't know, but I cannot put the following except for in some case scenarios.
onclick="check(param1,param2,param3)"

cmiskow
October 13th, 2005, 06:09 PM
I now work with JavaScript and PHP a lot. I am not sure why I encounter this, maybe it is a Firefox thing, either way I don't know, but I cannot put the following except for in some case scenarios.
onclick="check(param1,param2,param3)"

Never encountered this before, but I don't use PHP much and the JS I write at work is mostly only required to be IE compatible. Thanks :)