Skip to content

Instantly share code, notes, and snippets.

@stefanbackor
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanbackor/11211193 to your computer and use it in GitHub Desktop.
Save stefanbackor/11211193 to your computer and use it in GitHub Desktop.
// Speed up your webapp click response using jQuery..
/*
Use case: Lets suppose we are using XMLHttpRequest Ajax calls for page content change - expecting html return to just redraw page container, not whole layout
Solution: Use mousedown instead of click if possible. Don't forget to bypass if any function key or other than left click pressed.
*/
$(document).on("mousedown","a[href^='/']",basicAjaxEvent);
$(document).on("click","a[href^='/']",basicAjaxEvent);
function basicAjaxEvent(e){
var ret = undefined;
var callback=doYourBasicAjaxPageContentRequestAndReturnTrueIfSuccess_function;
$(this).data('was-mouse-down',ret);
if(e.altKey==false&&e.ctrlKey==false&&e.shiftKey==false&&e.metaKey==false&&(e.which==0||e.which==1)&&typeof callback == "function"/*&&!isLameBrowser()*/) {
if(e.type=="mousedown"){
ret = !callback($(this));
$(this).data('was-mouse-down',ret);
return ret;
}
else if(e.type=="click"){
return ($(this).data('was-mouse-down' == true) ? true : false;
}
else {
return false;
}
}
else if(e.type=="click"&&$(this).attr("href")!=undefined&&$(this).attr("href")!="") {
return callback($(this));
}
else if($(this).attr("href")!=undefined&&$(this).attr("href")!="") {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment