Skip to content

Instantly share code, notes, and snippets.

@jwalsh
jwalsh / getStyle.js
Created May 2, 2012 12:30
Third-Party JavaScript version 8: Listing 3.6: Finding the value of an element's style property
function getStyle(node, property, camel) {
var value;
if (window.getComputedStyle) {
value = document.defaultView
.getComputedStyle(node, null)
.getPropertyValue(property);
} else if (node.currentStyle) {
value = node.currentStyle[property] ?
node.currentStyle[property] :
node.currentStyle[camel];
function makeRequest(url, method) {
var xhr;
if (typeof XMLHttpRequest === "undefined") {
return null;
}
xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
return xhr;
[ 1, 5, 9, 2, 5, 0 ].reduce(function(p, c, i, a){ return c > p ? c : p; });
@jwalsh
jwalsh / pollution.js
Created June 5, 2012 18:11
Show namespace pollution
(function(global, undefined) {
// name, prefix, depth
var print = function(n, p, d) {
var b = p[n];
// Allow base indenting based on the depth
var response = '\n' +
(new Array(d).join(' ')) +
n +
' (' + (typeof b) + ')';
(function(global, undefined) {
var MAXDEPTH = 10;
// name, prefix, depth
var print = function(n, p, d) {
if (d > MAXDEPTH) {
return new Error('MAXDEPTH exceeded');
}
console.log(n, p, d);
var b = p[n];
function getNavTiming(name) {
if (!window.performance || !window.performance.timing) return 'unsupported';
var t = window.performance.timing;
var startName = name + 'Start', endName = name + 'End';
if (name.indexOf('dom') == 0 && name.indexOf('domain') == -1) {
startName = 'responseStart'; endName = name;
}
switch (name) {
case 'load': startName = 'responseStart'; endName = 'loadEventEnd'; break;
case 'connect':
var page = require('webpage').create(),
address, output, size;
if (phantom.args.length < 2 || phantom.args.length > 3) {
console.log('Usage: ' + phantom.scriptName + ' URL filename');
phantom.exit();
}
var start = new Date();
(function(global, undefined) {
// Protect and log errors
try {
var log = (typeof console.log === 'function') ?
console.log :
function(m) {};
var MAXDEPTH = 2;
// name, prefix, depth
var print = function(n, p, d) {
<h1>Summary</h1>
<p>My example project</p>
<h1>Code</h1>
<div style="">
<script src="https://gist.github.com/3094001.js"> </script>
</div>
$(element)