Trying to use return false to not allow link to be accessed after onclick event is fired


T

Terry

Hi people.

I took on a job where the client wanted his banner to show up at the
bottom of the browser 30 seconds after the document has loaded. He
also wanted the ability for the banner to be closed if the user
clicks
on close.

I got everything to work except for the getting the banner to close.

I tried <a href="" onclick='closeAd(); return false'>Close</a>


but with no luck the link is still accessed and the page is therefore
reloaded.


The url is http://theamazing.onlinewebshop.net/traditional/traditional-kitchens....


Thanks,
Terry
 
Ad

Advertisements

E

Erwin Moller

Terry said:
Hi people.

I took on a job where the client wanted his banner to show up at the
bottom of the browser 30 seconds after the document has loaded. He
also wanted the ability for the banner to be closed if the user
clicks
on close.

I got everything to work except for the getting the banner to close.

I tried <a href="" onclick='closeAd(); return false'>Close</a>


but with no luck the link is still accessed and the page is therefore
reloaded.

Hi Terry,

Your construct looks ok, but I expect that your function closeAd() fails
horribly, and thus 'return false' is never reached.
Did you check your errorconsole?
(In case you are using IE, switch to FF today and have an errorconsole.)

Regards,
Erwin Moller
 
T

Terry

Hi Terry,

Your construct looks ok, but I expect that your function closeAd() fails
horribly, and thus 'return false' is never reached.
Did you check your errorconsole?
(In case you are using IE, switch to FF today and have an errorconsole.)

Thanks, I got it working. It turns out that I was using was using an
older copy of my javascript file. When I cleared the cache in Firefox
I was fine.

Terry
 
S

Steve Swift

Thanks, I got it working. It turns out that I was using was using an
older copy of my javascript file.

If you always want to suppress the corresponding link, you could change
closeAd() to "return false", then use "return closeAd()" in your HTML.

It's just a little simpler. It also means that you could, if you wanted,
decide inside closeAd() if you wanted to follow the link or not.

I was taught this trick a couple of weeks ago, so now I've passed it on.
 
R

Robert FISHER

Terry said:
I tried <a href="" onclick='closeAd(); return false'>Close</a>

What the onclick handler returns isn't the issue in this case, it's the
href. This is just the thing the "void(0)" feature of the javascript URL
protocol is for.

<a href="javascript:void(0)" onclick="closeAd()">Close</a>
 
D

David Mark

What the onclick handler returns isn't the issue in this case, it's the

The issue is that the closeAd function is having an error, thereby
stopping anything from being returned from the inline handler.
href. This is just the thing the "void(0)" feature of the javascript URL
protocol is for.

You never need to use that in an href.
 
Ad

Advertisements

T

Thomas 'PointedEars' Lahn

Robert said:
What the onclick handler returns isn't the issue in this case, it's the
href. This is just the thing the "void(0)" feature of the javascript URL
protocol is for.

That is complete utter nonsense. `void(0)' is _not_ "a feature of the
javascript URL protocol", as firstly there is no such thing (`javascript:'
syntax can not in any way be considered an URL, nor are there "URL
protocols" [there are _URI schemes_]), and secondly `void' is an operator
defined in ECMAScript and supported in all known ECMAScript implementations,
of which `javascript:' syntax merely is a possibility of use.

Intrinsic event handler attributes like `onclick' (here: for the `click'
event) are another way that, in contrast to `javascript:', is standardized.

Not really standardized but well-supported is the cancellation of events by
returning certain (boolean) values to event handlers, in this case `false'
to cancel the `click' event. Canceling an event this way is usually (and
in this case) equal to preventing the default action for the event of the
element.

In this case, the default action is navigation (as it is a visible hypertext
link), so the `href' attribute is disregarded and navigation does not take
place when the link is clicked. Unless, of course, there is a fatal error
in the `closeAd' method when script execution stops there and so `return
false' is not executed.
<a href="javascript:void(0)" onclick="closeAd()">Close</a>

You have yet to understand how event handlers work, among other things.

Please consult the FAQ and some reference material before you provide any
further advice.


PointedEars
 

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

Top