Skip to content

Instantly share code, notes, and snippets.

@yuval-a
yuval-a / getjsonp
Created May 2, 2015 01:52
getJSONP in vanilla JS
function getJSONP(url, success) {
var ud = 'callback_' + + Math.floor(Math.random()*1000000),
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
window[ud] = null;
success && success(data);
@yuval-a
yuval-a / xmlhttp_post
Created May 2, 2015 01:20
XMLHttpRequest POST
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send("param=bla&param=bla");
@yuval-a
yuval-a / smooth_scroll_jquery
Last active August 29, 2015 14:19
JQuery - Smooth scroll to any anchor
$window.load(function() {
var scrollOffset = ; // Do some padding calculations etc. - this will be substracted from the target item top position
// Assign to any links/buttons with '#something' in their href.
$('#nav a').on('click',function(e) {
var $href = $(this).attr('href'),
hi = $href.indexOf('#');
// If no hash part return;
if (hi==-1) return true;
e.preventDefault();
// The double decodeURI is to support non-english
@yuval-a
yuval-a / HTML_HEAD
Last active August 29, 2015 14:19
HTML Head template
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="[description here]" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>[title here]</title>
<link rel="stylesheet" href="css/normalize.css" />
<link rel="stylesheet" href="css/style.css" />
@yuval-a
yuval-a / js-micro.js
Last active November 28, 2024 08:13
Javascript micro-optimizations
NOTE: This document is OLD - and most of the tips here are probably outdated, since newer versions of Javascript have been
released over the years - with newer optimizations and more emphasis on optimizing newly supported syntax.
// Array literal (= []) is faster than Array constructor (new Array())
// http://jsperf.com/new-array-vs-literal/15
var array = [];
// Object literal (={}) is faster than Object constructor (new Object())
// http://jsperf.com/new-array-vs-literal/26