Skip to content

Instantly share code, notes, and snippets.

Avatar
🤖

Rémi Breton remibreton

🤖
  • Ubisoft
  • Montréal
View GitHub Profile
View JavaScript: jQuery country finder
$.getJSON('//country-finder.herokuapp.com/?callback=?', function (data) {
return data.geo.country.toLowerCase()
});
View JavaScript: AJAX GET request
var get = function(url, callback, error, format){
if(!format) format = "json";
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
if(typeof callback === "function") callback(format == "json" ? JSON.parse(xmlhttp.responseText) : xmlhttp.responseText);
} else {
if(typeof error === "function") error(format == "json" ? JSON.parse(xmlhttp.responseText) : xmlhttp.responseText);
View JavaScript: Client-side password generator
// Change 15 to the password length
(function(e){var t="abcdefghijklmnopqrstuvwxz1234567890!@#$%?".split(""),n="";do{var r=t[Math.floor(Math.random()*t.length)];n+=Math.random()>.5?r.toUpperCase():r.toLowerCase()}while(n.length<e);return n})(15)
View Data: Country codes (ISO 3166-1 alpha-2)
'AF','AX','AL','DZ','AS','AD','AO','AI','AQ','AG','AR','AM','AW','AU','AT','AZ','BS','BH','BD','BB','BY','BE','BZ','BJ','BM','BT','BO','BA','BW','BV','BR','IO','BN','BG','BF','BI','KH','CM','CA','CV','KY','CF','TD','CL','CN','CX','CC','CO','KM','CG','CD','CK','CR','CI','HR','CU','CY','CZ','DK','DJ','DM','DO','EC','EG','SV','GQ','ER','EE','ET','FK','FO','FJ','FI','FR','GF','PF','TF','GA','GM','GE','DE','GH','GI','GR','GL','GD','GP','GU','GT','GG','GN','GW','GY','HT','HM','VA','HN','HK','HU','IS','IN','ID','IR','IQ','IE','IM','IL','IT','JM','JP','JE','JO','KZ','KE','KI','KR','KW','KG','LA','LV','LB','LS','LR','LY','LI','LT','LU','MO','MK','MG','MW','MY','MV','ML','MT','MH','MQ','MR','MU','YT','MX','FM','MD','MC','MN','ME','MS','MA','MZ','MM','NA','NR','NP','NL','AN','NC','NZ','NI','NE','NG','NU','NF','MP','NO','OM','PK','PW','PS','PA','PG','PY','PE','PH','PN','PL','PT','PR','QA','RE','RO','RU','RW','BL','SH','KN','LC','MF','PM','VC','WS','SM','ST','SA','SN','RS','SC','SL','SG','SK','SI','SB','SO','ZA','GS','ES'
View JavaScript: Get element offset
function getPosition(element){
for(var lx = 0, ly = 0; element != null; lx += element.offsetLeft, ly += element.offsetTop, element = element.offsetParent);
return {x: lx, y: ly};
}
View JavaScript: Vanilla removeClass()
function removeClass(element, cls){
if(typeof element.classList !== "undefined"){
element.classList.remove(cls);
return element;
} else {
if(element.className.indexOf(cls) != -1){
element.className = element.className.replace(cls, '');
}
return element;
}
View JavaScript: Vanilla addClass()
function addClass(element, cls){
if(typeof element.classList !== "undefined"){
element.classList.add(cls);
} else {
element.className += element.className.indexOf(cls) == -1 ? cls : ' ' + cls;
}
return element;
}
View HTML: Mobile first stylesheets
<link rel="stylesheet" href="style.css" media="all">
<link rel="stylesheet" href="style.tablet.css" media="all and (min-width: 570px)">
<link rel="stylesheet" href="style.desktop.css" media="all and (min-width: 960px)">
<!--[if (lt IE 9) & (!IEMobile)]>
<link rel="stylesheet" href="style.tablet.css" media="all">
<link rel="stylesheet" href="style.desktop.css" media="all">
<![endif]-->
View JavaScript: Get total document height
var h = (function(b, d){
return Math.max(b.scrollHeight, b.offsetHeight, d.clientHeight, d.scrollHeight, d.offsetHeight);
})(document.body, document.documentElement);
View CSS: Conditional normalize.css loading
<!--[if lte IE 8]> <link rel="stylesheet" href="css/libs/normalize.1.1.2.min.css"> <![endif]-->
<!--[if gt IE 8]> <!--> <link rel="stylesheet" href="css/libs/normalize.2.1.2.min.css"> <!--<![endif]-->