Forum Moderators: open

Message Too Old, No Replies

Javascript Methods

calling them with the () and without

         

sned

6:54 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



So here's an interesting question:

I've been fooling around with xmlhttprequest, I've had problems with readyState for a while.

I have a function to process the requests, something like:

function processReq(){
if(http.readyState == 4){
// do stuff
}
}

Here's the quandary:

If I call the function like this:
http.onreadystatechange = processReq();
Nothing is returned, the readyState just seems to stay at 1.

But, if I do this:
http.onreadystatechange = processReq;
everything is just fine and dandy.

So, what's the difference between processReq() and processReq?

Thanks!
-Sned

sned

7:25 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Also, another quick question:

Is there anyway to use javascript to select a certain number of letters a textbox?

Say for instance, the textbox contains "elephant", I want the script to only select "phant".

I'm trying to work on an autocomplete thing, selecting people from a database based on what a user types in the text box.

Thanks!

-sned

<edit>
I found these methods in Google's suggest javascript:
createTextRange
setSelectionRange

I think those will do the trick!
</edit>

[edited by: sned at 7:45 pm (utc) on June 20, 2005]

j4mes

7:36 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Hey sned,

But, if I do this:
http.onreadystatechange = processReq;
everything is just fine and dandy.

I could be wrong but i think it's because processReq is being called as a variable, which is being assigned to http.onreadystatechange, whereas when you use processReq(), it assigns http.onreadystatechange the value of the function, which is generally nothing, hence (I assume) the breakage.

Then again I could be totally off, but there are plenty of JavaScript gurus about to help out :-)

J.

JerryOdom

7:47 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



I don't program in javascript alot but I'll guess.

They're being interpreted as two different functions(class issue/prototyping issue) or perhaps using the () changes the data type of the returned information.

sned

8:57 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Ok, that makes sense to me. Thanks for your replies!
-sned

Rambo Tribble

2:52 am on Jun 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, as j4mes has suggested, when you are assigning a method or a function to an event handler you simply use the function's name, without the parentheses. The parentheses are used when invoking the function and, of course, within the function's declaration syntax.