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
Post a Comment