hypernetware — 2011-05-30T00:12:16-04:00 — #1
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.
cyberalien — 2011-05-30T03:53:10-04:00 — #2
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.
paul_wilkins — 2011-05-30T04:00:24-04:00 — #3
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.
hypernetware — 2011-05-30T18:41:51-04:00 — #4
Thanks for clearing that up for me.