Skip to content

Instantly share code, notes, and snippets.

@arturparkhisenko
arturparkhisenko / fitelement.js
Last active September 21, 2015 18:16
fitelement(something with aspect), script for rAF.js
var fitElement = function fitElement(elementSelector, elementAspect) {
var element = (typeof elementSelector === 'string' || elementSelector instanceof String) ? document.querySelector(elementSelector) : elementSelector,
parW = element.parentNode.clientWidth,
parH = element.parentNode.clientHeight,
parAsp = parW / parH,
aspect = elementAspect || 16 / 9,
elW, elH;
if (aspect > parAsp) {
elW = parW;
elH = elW / aspect;
@arturparkhisenko
arturparkhisenko / browserInfo.js
Last active January 22, 2016 18:09
Browser-and-OS-detect
function BrowserInfo() {
this.supported = false;
this.isBrowser = true;
this.webBrowser = null;
this.webBrowserVer = null;
this.os = 'unknown os';
var detectedUA = null;
if (typeof window === 'undefined' || !window.navigator) {
@arturparkhisenko
arturparkhisenko / remove.js
Last active August 19, 2017 06:20
function to remove by selector
/**
* remove - removes DOM elements
* working with: usual dom element, selector, jQuery object, Polymer object
* @param {string | object} selectorOrObject - dom object
*/
function remove(selectorOrObject) {
if (!selectorOrObject) {
return;
}
@arturparkhisenko
arturparkhisenko / js-remove-hash.js
Last active September 21, 2015 18:29
remove hash from url
//source: http://stackoverflow.com/questions/4508574/remove-hash-from-url
//Best is Homero Barbosa's answer below:
history.pushState('', document.title, window.location.pathname);
//... or, if you want to maintain the search parameters:
history.pushState('', document.title, window.location.pathname + window.location.search);
//Old, do not use, badwrongfun:
// var loc = window.location.href,
// index = loc.indexOf('#');
// if (index > 0) {
@arturparkhisenko
arturparkhisenko / resize.js
Created September 28, 2015 20:14
resize and throttle with rAF
//https://developer.mozilla.org/en-US/docs/Web/Events/resize
;
(function() {
var throttle = function(type, name, obj_) {
var obj = obj_ || window;
var running = false;
var func = function() {
if (running) {
return;
}
@arturparkhisenko
arturparkhisenko / append-prepend.js
Created October 3, 2015 20:24
append-and-prepend-elements-js
//Setup
let el = document.getElementById('thingy');
let elChild = document.createElement('div');
elChild.innerHTML = 'Content';
//Append
el.appendChild(elChild);
//Prepend
el.insertBefore(elChild, el.firstChild);
@arturparkhisenko
arturparkhisenko / js-overwrite-back-button.js
Created October 20, 2015 18:48
overwrite-back-button-history-api
//source https://stackoverflow.com/questions/6359327/detect-back-button-click-in-browser
window.onload = function() {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function() {
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
} else {
@arturparkhisenko
arturparkhisenko / js-clone.js
Last active May 30, 2018 09:32
clone-js-object-array-etc
const clone = data => {
if (typeof data !== 'object' || data === null) {
return data;
}
const copy = data instanceof Array ? data.constructor() : Object.create(Object.getPrototypeOf(data));
for (const [key, value] of Object.entries(data)) {
copy[key] = clone(value);
}
return copy;
};
const prefetch = document.createElement('link');
prefetch.setAttribute('rel', 'prefetch');
prefetch.setAttribute('href', url);
document.head.appendChild(prefetch);
@arturparkhisenko
arturparkhisenko / iterate-obj-nodeList.js
Last active April 12, 2017 17:22
iterate-through-object-properties-or-nodeList
// speed winners is: for-with-cached-length OR for-of-with-let > map > forEach
for (let key of iterable) {
// do stuff
}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
// do stuff
}