SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setting a cookie when user clicks a link and prevent default

    I'm trying to set a cookie when a user clicks a hyper link.

    Do you think I should prevent the default behavior of pressing a link first, and then set the cookie?

    Or could I just set the cookie on a hyper link click event, and hope the client sets the cookie, before they are directed to another page.

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To prevent default, all you have to do is return false in onclick event. So event could be like this: onclick="set_cookie(); return false;"

    If you want to set cookie before user is redirected, just set it and then return true: onclick="set_cookie(); return true;"

    It isn't done asynchronously, first you set cookie, then you allow/disallow default action by returning true/false, so cookie will always be set.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by CyberAlien View Post
    It isn't done asynchronously, first you set cookie, then you allow/disallow default action by returning true/false, so cookie will always be set.
    Returning true isn't necessary either.

    To expand on answering the OP's question, the events are always processed first before the default action takes place.
    So no, you don't need to prevent the default behaviour.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for clearing that up for me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •