JavaScript: View Source code without Comments

One my clients had a requirement of viewing an object while debugging, and they wanted to view only the source code, without comments.

I first thought of looking out for a similar approach implemented in one of the javascript minifiers. But on further digging, I found that JavaScript has a toSource() method. The drawback is it is gecko-only and  works only in Firefox. Since the source code had to viewed by only the developers who were debugging the code, ‘works-only-on-firefox’ was accepted.

Here’s the solution in case you have a similar requirement and are ok with a non-standard solution.

javascript tosource

javascript tosource

As you can see, toSource returned only the JavaScript source and not comments. I really wish this was cross-browser. I will probably try my hands on the JSON.stringify method and see if something similar can be achieved.

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Phong Thai said...

wow, good & cool discovery, thank for sharing, let me try it; but it'll display source code on in-line mode?