Skip to content

Instantly share code, notes, and snippets.

View droid001's full-sized avatar

droid001

View GitHub Profile
function generateRandomID() {
return Math.floor(Math.random() * 9e99).toString(36);
}
@droid001
droid001 / img-cover.js
Created October 14, 2015 07:16
Make inline img's cover container
// From http://paulbrowne.fi/2015/01/31/background-image-properties-inline-images
<div class="icovr">
<img href="some-image.jpg" alt="" >
</div>
<script>
function imgcover(){
// target all images in this container
@droid001
droid001 / isArray.js
Created October 14, 2015 07:23
Checks if object is type array
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
@droid001
droid001 / integer_from_float.txt
Last active January 4, 2016 12:06
float to integer conversion
// found at http://ejohn.org/blog/asmjs-javascript-compile-target/
let integer_variable = float_variable | 0;
@droid001
droid001 / urlPathKey.txt
Created January 25, 2016 09:29
URL path key from the current location
const urlpathKey = window.location.pathname.replace(/^\//, '').split('/').shift();
@droid001
droid001 / children.js
Created March 10, 2016 07:39
Returns all child elements of a given element which match a provided selector
/**
* @param {Element} el
* @param {string} selector
* @return {Element[]}
*/
h.children = function(el, selector) {
var selectors = null,
children = null,
childSelectors = [],
@droid001
droid001 / closetParent.js
Last active April 12, 2016 13:00
Returns the closest parent element to a given element matching the provided selector, optionally including the element itself
/**
* @param {Element} el
* @param {string} selector
* @param {boolean} [includeSelf]
* @return {Element|null}
*/
const closestParent = function(el, selector, includeSelf) {
@droid001
droid001 / index.js
Created March 10, 2016 07:46
Returns the index of a given element in relation to its siblings, optionally restricting siblings to those matching a provided selector.
/**
* @param {Element} el
* @param {string} [selector]
* @return {number}
*/
h.index = function(el, selector) {
var i = 0;
while ((el = el.previousElementSibling) !== null) {
@droid001
droid001 / browserDetection.js
Created March 14, 2016 09:34
Brwoser detection through duck typing
// From http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// At least Safari 3+: "[object HTMLElementConstructor]"
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
@droid001
droid001 / documentHeight.js
Last active March 30, 2016 12:29
document/body height
const documentHeight = function() {
var body = document.body,
html = document.documentElement,
height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight )
;
return height;
}