Skip to content

Instantly share code, notes, and snippets.

@tkissing
Created December 18, 2013 20:19
Show Gist options
  • Save tkissing/8029250 to your computer and use it in GitHub Desktop.
Save tkissing/8029250 to your computer and use it in GitHub Desktop.
Console script to count and list out globals on your page
/*jslint browser:true, eqeq:true, forin:true, sloppy: true*/
(function (window, document) {
var differences = {},
exceptions = ['addEventListener', 'document', 'location', 'navigator', 'window'],
globals = [],
ignoreList = (window.prompt('Ignore filter (comma sep)?', '$,jQuery,ko') || '').split(','),
iframe = document.createElement('iframe'),
i;
for (i in window) {
if (exceptions.indexOf(i) < 0 && ignoreList.indexOf(i) < 0 && (String(window[i]) != '[object Window]')) {
differences[i] = {
'type': typeof window[i],
'val': window[i]
};
}
}
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.src = 'about:blank';
iframe = iframe.contentWindow || iframe.contentDocument;
for (i in differences) {
if (iframe[i] === undefined) {
globals.push(i);
}
}
window.console.log('You have these %s globals', globals.length, globals);
}(window, window.document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment