Skip to content

Instantly share code, notes, and snippets.

View addyosmani's full-sized avatar
🎯
Focusing

Addy Osmani addyosmani

🎯
Focusing
View GitHub Profile
/**
Code copyright Dustin Diaz and Ross Harmes, Pro JavaScript Design Patterns.
**/
// Constructor.
var Interface = function (name, methods) {
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
@addyosmani
addyosmani / visibly.js
Created August 3, 2011 12:44
Cross-browser Page Visibility API polyfill
/*!
* isVis - v0.5.5 Aug 2011 - Page Visibility API Polyfill
* Copyright (c) 2011 Addy Osmani
* Dual licensed under the MIT and GPL licenses.
*/
(function () {
window.visibly = {
b: null,
q: document,
@addyosmani
addyosmani / vendorPrefix.js
Created August 8, 2011 06:13 — forked from danheberden/vendorPrefix.js
JS snippet for getting the current vendor prefix of a CSS(3) property
function getPrefix( prop ){
var vendorPrefixes = ['Moz','Webkit','Khtml','O','ms'],
style = document.createElement('div').style,
upper = prop.charAt(0).toUpperCase() + prop.slice(1),
pref, len = vendorPrefixes.length;
while( len-- ){
if((vendorPrefixes[len] + upper) in style){
pref = (vendorPrefixes[len]);
}
}
@addyosmani
addyosmani / vendorPrefix.js
Created August 8, 2011 06:13 — forked from danheberden/vendorPrefix.js
Get the vendor prefix for a property in a specific context.
function getPrefix(prop, context) {
var vendorPrefixes = ['moz', 'webkit', 'khtml', 'o', 'ms'],
upper = prop.charAt(0).toUpperCase() + prop.slice(1),
pref, len = vendorPrefixes.length,
q;
while (len--) {
q = vendorPrefixes[len];
if (context.toString().indexOf('style')) {
q = q.charAt(0).toUpperCase() + q.slice(1);
@addyosmani
addyosmani / objectClone.js
Created August 9, 2011 18:56
Some random fun with object cloning
/*
* objectClone.js (c) Addy Osmani, 2011.
* Do whatever license.
* Thanks to gf3 and ben_alman for tips that helped improve.
*/
function objectClone(q){
var n = (q instanceof Array) ? [] : {},i;
for (i in q) {
if (Object.prototype.toString.call({}) == Object.prototype.toString.call( q[i] )) {
@addyosmani
addyosmani / LICENSE.txt
Created August 10, 2011 13:39 — forked from 140bytes/LICENSE.txt
140byt.es -- objectClone
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@addyosmani
addyosmani / examples.js
Created August 14, 2011 09:57
Some examples for my large-scale JavaScript talk
////Module Pattern based on a singleton
var basketModule = (function() {
var basket = []; //private
return { //exposed to public
addItem: function(values) {
basket.push(values);
},
getItemCount: function() {
return basket.length;
@addyosmani
addyosmani / mobileURLs.js
Created August 17, 2011 11:32 — forked from louisremi/mobileURLs.js
matchMedia Demo
// matchMedia is supported and the device has a small screen
if ( "matchMedia" in window
&& window.matchMedia( "(max-device-width: 800px)" )
) {
Array.prototype.forEach.call(
// for each element matching the selector...
document.querySelectorAll(
"a[href*='youtube.com/watch'], a[href*='flickr.com']"
),
// ...replace the href with their mobile equivalent
@addyosmani
addyosmani / matchMedia.js
Created August 17, 2011 11:33 — forked from paulirish/matchMedia.js
media query check - matchMedia - moved to https://github.com/paulirish/matchMedia.js
/*
* matchMedia() polyfill - test whether a CSS media type or media query applies
* authors: Scott Jehl, Paul Irish, Nicholas Zakas
* Copyright (c) 2010 Filament Group, Inc
* MIT license
* dev.w3.org/csswg/cssom-view/#dom-window-matchmedia
* in Chrome since m10: http://trac.webkit.org/changeset/72552
*/
@addyosmani
addyosmani / commonjs.module-boilerplate.js
Created August 20, 2011 22:51
commonjs-module-boilerplates
/*
Courtesy of Sitepen
*/
/*
First, let’s look at very lightweight, but powerful boilerplate, an adapter that will let you write a module for both AMD and CommonJS standard module systems, using AMD style dependency declarations. This combination allows us to create modules that can be used both in browser and server environments:
*/