views:

65

answers:

4

I have a really, really nasty bit of JS code that I've inherited. The code is quite long, and quite obtrusive. The functions defined are all about a thousand or so lines each...

Anyway, since there isn't a call to anything as elegant as onload, I am trying to figure out how what is on the screen gets on the screen. Thus I need a way to separate the fly crap from the pepper as it were...

I need to be able to find the code that is not contained in functions and is just called "out in the wild" so I can find out where this silly program begins...does anybody know a good way to do this?

+1  A: 

Firefox & Firebug -- set breakpoints and step through the code as it executes. The other thing you could do is start refactoring into classes/objects and see what breaks in the console (due to no references). I'd probably take a run through the debugger first, though.

tvanfosson
I'd say the code is too messy for that...it wasn't exactly written by a JS guy...
leeand00
...it was written by a systems programmer.
leeand00
A: 

I might try using a regular expression to dump what's floating. That could get complicated, though, depending on the internal structure. Other than that, I can only think of trying firebug to step through the code. Either way, with giant monolithic files, you're in for a lot of work.

geowa4
I like this idea...but I don't know where to find a regular expression for selecting functions (basically I'd need to invert that)
leeand00
A: 

You should try a debugger, look at 50 Useful JavaScript Tools

rodrigoap
+1  A: 

A brute-force method could be to include some script at the very top (of the script includes) to set the document object to undefined/null so that any references to its methods will cause a runtime error. That may help you spot the line # and filename of the first bit of code that tries to get something on the screen.

Ates Goral