Dump Global Leaks in 140byte.es JavaScript

Debugging helper dumps all variables that leaked into the global scope.

Clean JavaScript code does not leak into the global variable scope. That's why we use strange looking stuff like (function() { var a = 'I am local'; })(); to execute code in a local scope. Inside such scopes, we can declare and use variables without conflict.

Since you often don't get a warning when using undeclared variables, I wrote an 140byt.es snippet to detect leaking variables. The idea is very simple and works in Opera and Firefox. Does not work in Internet Explorer.

Due to a bug in Firefox I had to add a try-catch block. Accessing or even checking the type of window.sessionStorage causes a "not supported" exception. It works fine without this in all other browsers. Another bug is that Firefox returns a getInterface method in the second call that was not there in the first call. I did not found out why this happens or how to prevent it. This is why I check for native functions. And (of course) there are problems in Internet Explorer. It does not store global variables in the window object so the function can't display anything. Replacing window with this does not help since it refers to the same object.

Created by Thiemo Mättig in January 2012 for 140byt.es.

Show the source of this page or visit the Gist at GitHub.