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
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 |
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
<!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" /> |
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
$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 |
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
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¶m=bla"); |
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
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); |
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
-webkit-transition: prop 0.2s ease; | |
-moz-transition: prop 0.2s ease; | |
-o-transition: prop 0.2s ease; | |
transition: prop 0.2s ease; |
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
// put 'req' on the elements of any required input fields. Don't use the 'required' attribute, otherwise the default action | |
// will trigger. | |
function onFormSubmit(form) { | |
var isValid = true; | |
Array.prototype.forEach.call(form.elements, function (e) { | |
if ((e.value.length == 0 || ! /\S/g.test(e.value)) && e.getAttribute('req') !== null && e.getAttribute('req') !== undefined) { | |
isValid = false; | |
e.value = ""; | |
if (e.getAttribute('req') == "") { |
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
var url = '/.../'; | |
var req = new XMLHttpRequest(); | |
req.open('GET', url, true); | |
req.onreadystatechange = function () { | |
if (req.readyState == 4 && req.status == 200) | |
console.log(req.responseText); | |
else | |
console.log("Error loading page"); | |
}; | |
req.send(); |
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
// A quick Javascript class for creating a countdown timer | |
// leadingZero is a boolean, onTimeCallback will be called on each second update with the timer string | |
function Countdown(hours,minutes,seconds, leadingZero, onTimeCallback) { | |
this.hours = hours; | |
this.minutes = minutes; | |
this.seconds = seconds; | |
this.countdownStr = hours+":"+minutes+":"+seconds; | |
this.onTime = onTimeCallback; | |
this.leadingZero = leadingZero; |
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
import React, { Component } from 'react' | |
export default class ComponentName extends Component { | |
constructor(props) { | |
console.log ("constructor"); | |
super(props); | |
this.state = { | |
}; | |
} |
OlderNewer