[jQuery] Anchor HREF update and delayed navigation
Hi,
I am using getJSON() to retrieve some data I want appended to a
clicked anchor's href before it navigates. I first tried binding the
update function to the anchor's click-event, but while the callback is
pending the browser navigates to the original href. I then tried both
unbind() and one() in combination with click(); basically, I override
the click-event completely with a preventDefault(), and then call the
element's click() in the callback.
The following example simulates the callback-delay, and updates the
href, but the final click() does not force navigation on the anchor as
I expected it should; I manually needed to click it again.
Does anyone have a solution? Thanks.
<script>
$(function(){
$('a').one('click',function(event){
test($(this));
event.preventDefault();
});
});
function test(link)
{
setTimeout(function(){callback(link,{href:'http://jquery.com'})},
1000);
}
function callback(link,json)
{
link.attr('href',json.href);
link.unbind('click');
link.trigger('click');
}
</script>
</head>
<body>
<a href="http://msn.com">test</a>
</body>