Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Trying to use return false to not allow link to be accessed after onclick event is fired

Reply
Thread Tools

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

 
 
Terry
Guest
Posts: n/a
 
      10-15-2007
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/...ional-kitchens....


Thanks,
Terry

 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      10-15-2007
Terry wrote:
> 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

>
> The url is http://theamazing.onlinewebshop.net/...ional-kitchens....
>
>
> Thanks,
> Terry
>

 
Reply With Quote
 
 
 
 
Terry
Guest
Posts: n/a
 
      10-15-2007
On Oct 15, 7:58 am, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.com> wrote:
> Terry wrote:
> > 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.)


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

 
Reply With Quote
 
Steve Swift
Guest
Posts: n/a
 
      10-16-2007
> 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.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
 
Reply With Quote
 
Robert FISHER
Guest
Posts: n/a
 
      10-18-2007
Terry wrote:
>
> 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>
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      10-19-2007
On Oct 18, 7:54 pm, Robert FISHER <n...@fisher.cx> wrote:
> Terry wrote:
>
> > 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


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.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-21-2007
Robert FISHER wrote:
> Terry wrote:
>> 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.


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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embedded <divs> with events: How to prevent the parent div's eventfrom being fired when the embedded div's event is fired? Num GG Javascript 2 11-17-2008 08:56 PM
Trying to use return false to not allow link to be accessed after onclick event is fired. Terry Javascript 1 10-15-2007 07:56 PM
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM
in a link onclick="someFunction(); return false;" should keep the browser from following the link? Jake Barnes Javascript 1 02-15-2006 05:26 AM
Linkbutton submits the form but OnClick event does not get fired Stan ASP .Net 1 12-03-2003 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57