ajax - Ajaxify hyperlink so on success will load content otherwise redirect as default -



ajax - Ajaxify hyperlink so on success will load content otherwise redirect as default -

i have <a href> hyperlink. used jquery clicking link load contents div in current page (i.e. remain in same page), , works now.

however want that, if request fails, link deed , go href url.

i tried e.preventdefault(); , homecoming false; in success: callback function, not in right scope. if place e.preventdefault() in calling function, can't reverse effect later.

here code:

$('a.more-link').click(function(e){ var postid=$(this).closest('div.post').attr("id").replace(/^post-(.*)$/,'$1'); var postcontent=$(this).parent(); $.ajax({ url:"?action=ajax_load_post&id="+postid, success:function(data){ postcontent.html(data); // can't access e.preventdefault, nor homecoming false; }, error:function(data){ } }); e.preventdefault(); });

don't worry preventdefault(), redirect user in error function this:

$('a.more-link').click(function(e){ var postid=$(this).closest('div.post').attr("id").replace(/^post-(.*)$/,'$1'); var postcontent=$(this).parent(); var _this = $(this); $.ajax({ url:"?action=ajax_load_post&id="+postid, success:function(data){ postcontent.html(data); }, error:function(data){ window.location = _this.attr('href'); homecoming false; } }); });

ajax jquery

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -