The context:
It is necessary for me to fetch the URL from the server when the user clicks the link. It is not available beforehand. The click on the link should work as normal, but with that fetched URL.
The procedure is as follows:
- The link contains
href="#"
before it is clicked - The link is clicked
- I hook into the click with
ng-mousedown
orng-click
, and fetch the URL from the server using a$http
request. - The href of the link is updated with the URL.
- The original click should complete (with that new url, not the
#
placeholder).
The problem comes on the last step. Since the $http
request is asynchronous, I suspect there is a timing issue. My suspicion is: If the server request is fast enough, it will go through, and the URL is changed before the original click goes through. If not, then the original click goes through and tries to go to the # placeholder URL. So nothing will happen when clicking the link.
I want to make the original click wait for the result of the $http
request to have returned with the URL. The reason the original click is important is that it could either be a left or a middle mouse button click (cmd+click), and I don't know which one, so it'd be hard to call it myself from the javascript.
So, any clever ideas on how can make the original click go through with the fetched URL?