<a id="a123" onclick="return chkfile(this.id);" etc

M

Mel Smith

Hi:

I'm trying to place and activate an 'onclick' parameter in an anchor
element, eg,:

<a id="a123" onclick="return chkfile(this.id);"
href="http://www.someurl.com/files/somefile.zip" >Check, then Get Some
File</a>

Problem: I don't seem to reach the javascript function
chkfile(cSomeId). I placed an alert box in it and also returned 'true', but
the alert never shows. Even returning 'false' doesn't stop the beginning of
the download. And the download works with or without the onclick function.

Can someone provide some corrective advice please ?

Thanks,
 
R

RobG

Hi:

    I'm trying to place and activate an 'onclick' parameter in an anchor
element, eg,:

    <a id="a123" onclick="return chkfile(this.id);"
href="http://www.someurl.com/files/somefile.zip" >Check, then Get Some
File</a>

    Problem:  I don't seem to reach the javascript function
chkfile(cSomeId).

Then you should make sure it is availble to be called when you want to
call it. Is the script file loaded, or is the function in the page?
Are errors being reported that you haven't told us about?
I placed an alert box in it and also returned 'true', but
the alert never shows. Even returning 'false' doesn't stop the beginning of
the download.

If the function isn't being called, its return value is irrelevant.
 And the download works with or without the onclick function.

Can someone provide some corrective advice please   ?

You don't say what errors you are getting (if any). I would start by
replacing the listener with:

onclick="alert(typeof chkfile); return false;"

If I got 'undefined' (or something other than 'function'), I'd set
about finding out why and fix that.

If I get 'function', I'd remove typeof to see if the value of chkfile
(i.e. the code) is what I expect. If not, I'd set about finding out
why and fix that.

If it shows the code I expect, then the code is silently failing for
some reason, start using debugging and profiling tools to see what's
going on. First step would be:

onclick="chkfile(...); return false;"

and so on...
 
T

Thomas 'PointedEars' Lahn

RobG said:
Mel Smith said:
I'm trying to place and activate an 'onclick' parameter in an anchor
element, eg,:

<a id="a123" onclick="return chkfile(this.id);"
href="http://www.someurl.com/files/somefile.zip" >Check, then Get Some
File</a>

Problem: I don't seem to reach the javascript function
chkfile(cSomeId). […]
And the download works with or without the onclick function.

Can someone provide some corrective advice please ?

You don't say what errors you are getting (if any). I would start by
replacing the listener with:

onclick="alert(typeof chkfile); return false;"

If I got 'undefined' (or something other than 'function'), I'd set
about finding out why and fix that.

If I get 'function', I'd remove typeof to see if the value of chkfile
(i.e. the code) is what I expect. If not, I'd set about finding out
why and fix that.

If it shows the code I expect, then the code is silently failing for
some reason, start using debugging and profiling tools to see what's
going on. First step would be:

onclick="chkfile(...); return false;"

and so on...

If the runtime environment features or supports a debugger (most do), it is
arguably more efficient to declare a wrapper function, pass that function
the necessary values, and set a breakpoint in the first line of that wrapper
function where you can easily inspect the pertinent values before they are
being applied:

function _wrapper(that)
{
return chkfile(that.id);
}

<… on…="return _wrapper(this)">…</…>

You are fortunate if you can use an IDE like Eclipse that allows you to
"Rename in file", thereby declaring arguments named like the identifiers in
the function, and renaming them with a few keystrokes.

You are even more fortunate if you have a debugger like that of Chromium
that can debug event-handler code, for example by use of the `debugger'
statement (formally specified since ES5FD):

<… on…="debugger; return chkfile(this.id);">…</…>


PointedEars
 
M

Mel Smith

Rob, Swifty and Thomas:

Got other 'family' problems today to resolve but hopefully tomorrow I
can get back to this 'onclick' problem of mine tomorrow.

Thank you for the suggestions.

(and NO Errors were reported as I ran IE7 on this little test problem.
The download was ready to commence whether (or not) I ran the chkfile()
function and whether or not I returned true or false.)

Anyway, maybe tomorrow I can puzzle again on this problem.

Thanks again.

-Mel Smith
 
M

Mel Smith

Hi Guys:

Well, I changed a bunch of things and now it works !

(but, damned if I know what I changed that did the job !)

Usually I make one change at a time and test the results, but this time,
I just stabbed at keys in frustration and consequently -- even though it
works correctly -- I should have my butt kicked !

Thanks and sorry for wasting your time.

(btw, the web site in question is www.whosaway.com with a password of
XHB. I'm working on the download screen that appears after this password is
entered. I'll be doing some XHR stuff with this download later.)


-Mel Smith
 
D

Denis McMahon

Hi Guys:

Well, I changed a bunch of things and now it works !

(but, damned if I know what I changed that did the job !)

probably a combination of:

a) using eventHandler="return function_name(arguments);"

and

b) function_name() returning false when you don't want to carry out the
default action.

Afair you absolutely *_MUST_* use "return function(args);" in the event
handler and not just "function(args);" or "function()" etc etc if you
want a "false" return value from the function to interrupt the call
stack that ends with the default action for the event.

Rgds

Denis McMahon
 
M

Mel Smith

Denis said:
b) function_name() returning false when you don't want to carry out the
default action.

Afair you absolutely *_MUST_* use "return function(args);" in the event
handler and not just "function(args);" or "function()" etc etc if you
want a "false" return value from the function to interrupt the call
stack that ends with the default action for the event.

Denis:

Yes, I *thought* I did all those things, but I must have missed one and
then corrected it without remembering what I did. Jeez :((

Anyway, thanks to you and the others that helped.

-Mel
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top