Here is a small function that (in firefox at least) inserts semi colons into a piece of javascript:
function insertSemicolons(jsCode) {
var f = new Function(jsCode);
var source = f.toSource();
var srcexp = /function anonymous\(\)[^{]*\{(.*)\}/im;
return source.match(srcexp)[1];
}
I expect the srcexp is quite broken and catches a bunch of cases im not expecting.
Ports
Opera
Opera supports Function.prototype.toString() that will normalise the function bodies code. Should be possible to update this code to work with toString rather than toSource (needs the regexp improved)
Internet Explorer
- 6 This is probably right out, i dont believe you can access the function body in IE6.
-
7 Probably not possible, while IE7 does provide a useful
Function.prototype.toString()it just provides you with the text you used, unnormalised.
Safari
I havent had a chance to test any version of safari yet.
Comments
Soooo, That’s what the kid’s are calling it these days? âJim
Haha, you beat me to it. It does sound pretty wrong. âmattw
you are both perverted sickos :P However, if you have a better name, feel free. – brehaut
