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.

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.

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.

Thanks for clearing that up for me.