Is there a 'Format Document' function for .js files?

Last post 06-11-2008 8:06 PM by Jeff.aspx. 12 replies.

Sort Posts:

  • Is there a 'Format Document' function for .js files?

    02-21-2007, 3:23 PM
    • Loading...
    • danmor
    • Joined on 11-04-2002, 9:48 PM
    • Redmond, WA
    • Posts 907
    • AspNetTeam
    The Web Forms editor (viewing ASPX source) has a command "Edit | Advanced -> Format Document".  This command doesn't exist when I open up a .js file for editing.  I have some scrpt that has been compacted, and is impossible to read.  Is there any way to automatically format the JavaScript so it is readable (insert line breaks and tabs, etc.)?
    -Dan
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Filed under:
  • Re: Is there a 'Format Document' function for .js files?

    02-21-2007, 5:11 PM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025
    Has it been compacted by human hands or by a programmatic obfuscator?
    Josh Stodola ← Come check out my blog!
  • Re: Is there a 'Format Document' function for .js files?

    02-21-2007, 5:55 PM
    • Loading...
    • danmor
    • Joined on 11-04-2002, 9:48 PM
    • Redmond, WA
    • Posts 907
    • AspNetTeam

    I'm not sure.  The variable names are human readable, but the spacing seems as if it was optimized by either a program or standard regular expressions.  Here's a sample:

    if(isRef)

    {this.dtRefresh=(new Date()).getTime();}}};function initACB(divid,Id,fw,fh)

    {adCont[Id].acbObj=null;adCont[Id].acb=(typeof(adCont[Id].acb)!='undefined')?adCont[Id].acb:true;var acbNeeded=(fw<acbMinWdth)?false:adCont[Id].acb;if(adCont[Id].acb)

    {var lbl=acbAdv;_dapUtils.acbSetLg();var acbLocLg=_dapUtils.acbLg!="en"&&_dapUtils.acbLg!="noacb";if(acbLocLg)

    {lbl="";acbGetLocStrs(adCont[Id].divid);}

     
    -Dan
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: Is there a 'Format Document' function for .js files?

    02-22-2007, 12:15 AM
    • Loading...
    • A1ien51
    • Joined on 05-06-2005, 6:46 PM
    • MD USA
    • Posts 2,836

    I just wrote this in a few minutes. It is very crude on how it works. It loops through the text looking for { or } or ; using a setTimeout. Reason for setTimeout is big loops freeze the browser.

     http://www.pascarello.com/examples/formatCode.html

    It will make some things look funny, but it is better than nothing.

     If I did not have the flu and I did not have tons of work to do and if it was before midnight I might have tried a little harder using split() with regular expressions to reduce a bunch of redundant calls that charAt() causes.

    Eric

    Coauthor of Ajax In Action [#1 Computer and Internet book on Amazon for 2006]

    If you get an answer to your question, please mark it solved so people don't waste time reading already answered questions!
  • Re: Is there a 'Format Document' function for .js files?

    02-22-2007, 2:03 AM
    • Loading...
    • Jeff.aspx
    • Joined on 09-11-2003, 10:16 PM
    • Redmond, WA
    • Posts 245
    • AspNetTeam
      Moderator

    Hey Dan,

    Formatting is NOT currently available for JavaScript in Visual Studio 2005 and current plans dictate that it won't be available for Visual Studio "Orcas" either.  I'm actually trying to fit the feature into our schedule at the moment.  I'd personally would really like to see this done.  Customer feedback from you guys would definitely help! =)

    Hope this helps,
    Jeff

     

    Jeff King
    Program Manager
    ASP.net
  • Re: Is there a 'Format Document' function for .js files?

    02-22-2007, 10:08 AM
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    Jeff,

    This day in age, there is an abundance of Javascript examples and references online.  A lot of people don't want to re-invent the wheel so they do a quick search and find a suitable javascript example in a matter of minutes and tweak it to perfectly meet their needs.  I have done this so many times, it is almost natural.  Unfortunately, the perfectionist in me forces me to re-do the indentation and whitespacing of these examples just to be uniform with my other javascript functions.  Because of this, I think it would be far more usable and practical to include Javascript formatting in Visual Studio.  If I had a choice of one formatting to be available, I would choose the Javascript formatting over any other.  It just makes sense to me because I very rarely have to format any other code.

    Tell Mr. Guthrie that it has to be done Surprise

    Josh Stodola ← Come check out my blog!
  • Re: Is there a 'Format Document' function for .js files?

    03-02-2007, 4:19 PM
    • Loading...
    • danmor
    • Joined on 11-04-2002, 9:48 PM
    • Redmond, WA
    • Posts 907
    • AspNetTeam
    Jeff.aspx:

    Hey Dan,

    Formatting is NOT currently available for JavaScript in Visual Studio 2005...Customer feedback from you guys would definitely help! =)

    I can tell you that this feature would help everyone who works on the Windows Live properties (http://spaces.live.com, http://local.live.com, etc) because we have so much client-side script on our pages now. If you want more information, or a more formal 'feature request' just send me an internal e-mail. I'm on the Spaces team now.
    -Dan
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Filed under: ,
  • Re: Is there a 'Format Document' function for .js files?

    03-02-2007, 4:36 PM
    • Loading...
    • SuperGhost
    • Joined on 12-29-2005, 4:58 PM
    • Palmdale CA, USA
    • Posts 399

    JavaScript formatting would be great. I've noticed over the past year our team has started using JavaScript a lot more. Would help out with coding standards especially now that ASP.NET AJAX is released. You know those brackets are all over the place:

    function multiply(one, two) {
    }

    function multiply(one, two)
    {
    }

    function multiply(one, two) { }

    ** Hope this helps!

    DennyDotNet.com
    Filed under: ,
  • Re: Is there a 'Format Document' function for .js files?

    05-09-2007, 9:51 AM
    • Loading...
    • andyb90
    • Joined on 03-10-2006, 11:03 PM
    • Posts 10

    It sure would be great to have a javascript formatter in Visual Studio.

    Right now, I cut/paste my javascript (in my .aspx/.ascx files) to my Visual Slick Edit editor, "Beautify" it, then paste it back into Visual Studio.  Sure would be nice to skip this step!

     Regards,

     Andy

  • Re: Is there a 'Format Document' function for .js files?

    10-04-2007, 10:22 PM
    • Loading...
    • rbb555
    • Joined on 10-05-2007, 2:19 AM
    • Posts 1

    Actually, maybe another way to approach this would be to have a "format as" function in the formatter - select a bunch of whatever, and apply the same formatting rules.

    This would be an awesome feature allowing a standard formatting schema for C++, C#, javascript etc etc etc 

    This would also solve the problem of embedded code sections in .aspx files and the like - 10 lines of javascript  - hey format that sucker as javascript/C#, 10 lines of HTML - format that sucker too -  some C# or VB code within <% %> tags - select that sucker and format as desired! Wow - I love it!

    Similarly those of use who are pedants and like to see our HTML formatted the same as our XML - would be really happy.


     
    Thanks for listening
     

     

     

  • Re: Is there a 'Format Document' function for .js files?

    12-28-2007, 12:16 AM
    • Loading...
    • jtoth55
    • Joined on 03-22-2005, 12:17 AM
    • Posts 133

    It certainly would make my life a lot easier right now, taking some 3rd party javascript code which was compressed by removing all of the spaces and carriage returns and having all of the code on one line... :(

    Justin Toth
    http://sportsalert.net
  • Re: Is there a 'Format Document' function for .js files?

    06-07-2008, 9:21 PM
    • Loading...
    • Bobzee
    • Joined on 11-22-2007, 7:06 PM
    • Baton Rouge
    • Posts 23

    Until Visual Studio adds that feature,  Try http://elfz.laacz.lv/beautify/  pretty cool online formatter for javascript

    Mark it as answer if you really think it helped you a bit.
    Bob Lakkakula
    http://nlakkakula.wordpress.com
  • Re: Is there a 'Format Document' function for .js files?

    06-11-2008, 8:06 PM
    • Loading...
    • Jeff.aspx
    • Joined on 09-11-2003, 10:16 PM
    • Redmond, WA
    • Posts 245
    • AspNetTeam
      Moderator

    By the way, with the release of Visual Studio 2008 SP1, "Format Document" for .js files will be supported.  Additionally, we will support formatting as you type.  More information here:

    http://blogs.msdn.com/webdevtools/archive/2008/05/12/introducing-jscript-formatting-in-vs-2008-sp1.aspx

    Jeff King
    Program Manager
    ASP.net
Page 1 of 1 (13 items)