Last active
June 23, 2016 21:26
-
-
Save davidejones/3674526e9f584a31c03a5719b2e90938 to your computer and use it in GitHub Desktop.
jquery detect if to use .live .delegate or .on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // example usage | |
| switch(checkVersion()) | |
| { | |
| case 1: | |
| signup_form.live("submit", false); | |
| break; | |
| case 2: | |
| signup_form.delegate("submit", false); | |
| break; | |
| case 3: | |
| signup_form.on("submit", false); // Shorthand for "return false" function() | |
| break; | |
| } | |
| switch(checkVersion()) | |
| { | |
| case 1: | |
| signup_form.die("submit"); | |
| break; | |
| case 2: | |
| signup_form.undelegate("submit"); | |
| break; | |
| case 3: | |
| signup_form.off("submit"); | |
| break; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function checkVersion() | |
| { | |
| // jQuery 1.3+ .live | |
| // jQuery 1.4.3+ .delegate | |
| // jQuery 1.7+ .on | |
| var vernums = $.fn.jquery.split('.'); | |
| if(vernums.length >=2 ) | |
| { | |
| // if there is no 3rd param set to zero | |
| if(vernums.length == 2) vernums[2] = 0; | |
| if(parseInt(vernums[0]) == 1) | |
| { | |
| //1.3.0 - 1.4.2 | |
| if( (parseInt(vernums[1]) == 3 && parseInt(vernums[2]) >= 0) || (parseInt(vernums[1]) == 4 && parseInt(vernums[2]) < 3) ) { | |
| // use live/die | |
| return 1; | |
| } else if ( (parseInt(vernums[1]) == 4 && parseInt(vernums[2]) >= 3) || (parseInt(vernums[1]) >= 5 && parseInt(vernums[1]) <= 6) || (parseInt(vernums[1]) == 7 && parseInt(vernums[2]) == 0) ) { | |
| // use delegate | |
| return 2; | |
| } else { | |
| // use on/off | |
| return 3; | |
| } | |
| } else { | |
| // anything greater than 1 try on/off | |
| return 3; | |
| } | |
| } else { | |
| // can't parse version try use on/off | |
| return 3; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment