Created
January 4, 2012 07:48
-
-
Save drewgillson/1559006 to your computer and use it in GitHub Desktop.
Lightweight Javascript testing tool
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
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
jQuery.cookie = function(name, value, options) { | |
// @author Klaus Hartl/[email protected] | |
if (typeof value != 'undefined') { | |
options = options || {}; | |
if (value === null) { | |
value = ''; | |
options.expires = -1; | |
} | |
var expires = ''; | |
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { | |
var date; | |
if (typeof options.expires == 'number') { | |
date = new Date(); | |
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); | |
} else { | |
date = options.expires; | |
} | |
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE | |
} | |
var path = options.path ? '; path=' + (options.path) : ''; | |
var domain = options.domain ? '; domain=' + (options.domain) : ''; | |
var secure = options.secure ? '; secure' : ''; | |
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); | |
} else { // only name given, get cookie | |
var cookieValue = null; | |
if (document.cookie && document.cookie != '') { | |
var cookies = document.cookie.split(';'); | |
for (var i = 0; i < cookies.length; i++) { | |
var cookie = jQuery.trim(cookies[i]); | |
// Does this cookie string begin with the name we want? | |
if (cookie.substring(0, name.length + 1) == (name + '=')) { | |
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | |
break; | |
} | |
} | |
} | |
return cookieValue; | |
} | |
}; | |
var DG_tester = (function(jQuery) { | |
// @author Drew Gillson/[email protected] | |
var my = {}; | |
var endpoint = '//yourdomain.com/endpoint/'; | |
var expires = 7; | |
my.assignAlpha = function(idx) { | |
return "ABCDEF"[(idx-1)]; | |
} | |
my.score = function(testId) { | |
var callback = jQuery.cookie(testId); | |
var alpha = this.assignAlpha(callback); | |
if (jQuery('#' + testId + '-score').length == 0 && jQuery.cookie(testId)) { | |
jQuery('body').append('<img style="display: none;" id="' + testId + '-score" src="' + endpoint + '?testId=' + testId + '&score=' + alpha + '"/>'); | |
} | |
} | |
my.test = function() { | |
var cookied = false; | |
testId = escape(arguments[0]); | |
if (jQuery.cookie(testId) == '' || !jQuery.cookie(testId)) { | |
var callback = Math.floor(Math.random()*(arguments.length-1))+1; | |
jQuery.cookie(testId, callback, { expires: expires }); | |
} | |
else { | |
cookied = true; | |
var callback = jQuery.cookie(testId); | |
} | |
arguments[callback].call(); | |
var alpha = this.assignAlpha(callback); | |
if (jQuery('#' + testId).length == 0) { | |
jQuery('body').append('<img style="display: none;" id="' + testId + '" src="' + endpoint + '?testId=' + testId + '&try=' + alpha + '"/>'); | |
} | |
} | |
return my; | |
}(jQuery)); | |
</script> |
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
<script type="text/javascript"> | |
jQuery(document).ready(function() { | |
DG_tester.test( | |
'red_or_green', | |
function() { | |
jQuery('#color').css('background-color','green'); | |
}, | |
function() { | |
jQuery('#color').css('background-color','red'); | |
} | |
); | |
}); | |
</script> | |
<div id="color">The background of this div will change color.</div> |
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
jQuery('#add-to-cart').click(function() { | |
DG_tester.score('red_or_green'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment