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

Discussion in 'Javascript' started by Terry, Oct 15, 2007.

  1. Terry

    Terry Guest

    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
    Terry, Oct 15, 2007
    #1
    1. Advertising

  2. Terry

    Erwin Moller Guest

    Re: Trying to use return false to not allow link to be accessed afteronclick event is fired

    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/traditional/traditional-kitchens....
    >
    >
    > Thanks,
    > Terry
    >
    Erwin Moller, Oct 15, 2007
    #2
    1. Advertising

  3. Terry

    Terry Guest

    On Oct 15, 7:58 am, Erwin Moller
    <> 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
    Terry, Oct 15, 2007
    #3
  4. Terry

    Steve Swift Guest

    Re: Trying to use return false to not allow link to be accessed afteronclick event is fired

    > 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
    Steve Swift, Oct 16, 2007
    #4
  5. Re: Trying to use return false to not allow link to be accessed afteronclick event is fired

    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>
    Robert FISHER, Oct 19, 2007
    #5
  6. Terry

    David Mark Guest

    On Oct 18, 7:54 pm, 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


    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.
    David Mark, Oct 19, 2007
    #6
  7. Re: Trying to use return false to not allow link to be accessed afteronclick event is fired

    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$>
    Thomas 'PointedEars' Lahn, Oct 22, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stan
    Replies:
    1
    Views:
    2,517
    Jacob Yang [MSFT]
    Dec 3, 2003
  2. Chilá
    Replies:
    0
    Views:
    130
    Chilá
    Sep 21, 2005
  3. Jake Barnes
    Replies:
    1
    Views:
    166
  4. Replies:
    11
    Views:
    293
    Thomas 'PointedEars' Lahn
    Dec 11, 2006
  5. Terry
    Replies:
    1
    Views:
    101
    Thomas 'PointedEars' Lahn
    Oct 15, 2007
Loading...

Share This Page