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

OUTPUT
  
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.

1 comment:

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

    ReplyDelete