Javascript Formatter?

Discussion in 'Javascript' started by John Dalberg, Feb 15, 2004.

  1. John Dalberg

    John Dalberg Guest

    I have spent 1/2 hour looking for a Javascript formatter with no luck.
    I am using a tool that produces Javascript statements in one long string
    and it's hard to read.

    Any recommendations? I see a .NET tool at codeproject but that site is
    down.
    John Dalberg, Feb 15, 2004
    #1
    1. Advertising

  2. John Dalberg

    Jim Ley Guest

    On Sun, 15 Feb 2004 13:47:44 -0800, John Dalberg <>
    wrote:

    >
    >I have spent 1/2 hour looking for a Javascript formatter with no luck.
    >I am using a tool that produces Javascript statements in one long string
    >and it's hard to read.
    >
    >Any recommendations?


    IE does a good job:

    alert(function() { /* LongString*/ })

    Jim.
    --
    comp.lang.javascript FAQ - http://jibbering.com/faq/
    Jim Ley, Feb 15, 2004
    #2
    1. Advertising

  3. John Dalberg

    John Dalberg Guest

    On Sun, 15 Feb 2004 22:24:26 GMT, Jim Ley wrote:

    > On Sun, 15 Feb 2004 13:47:44 -0800, John Dalberg <>
    > wrote:
    >
    >> [6 quoted line(s) supressed]

    >
    > IE does a good job:
    >
    > alert(function() { /* LongString*/ })


    It doesn't work. Long string as in a few lines in Wordpad.

    J.
    John Dalberg, Feb 16, 2004
    #3
  4. John Dalberg

    Jim Ley Guest

    On Sun, 15 Feb 2004 20:48:14 -0800, John Dalberg <>
    wrote:

    >On Sun, 15 Feb 2004 22:24:26 GMT, Jim Ley wrote:
    >
    >> On Sun, 15 Feb 2004 13:47:44 -0800, John Dalberg <>
    >> wrote:
    >>
    >>> [6 quoted line(s) supressed]

    >>
    >> IE does a good job:
    >>
    >> alert(function() { /* LongString*/ })

    >
    >It doesn't work. Long string as in a few lines in Wordpad.


    It does for me....

    Perhaps you should provide an example which demonstrates your problem.

    astyle is of course perhaps a more normal recommendation, but it's a
    little pointless when you already have something that does the job.

    Jim.
    --
    comp.lang.javascript FAQ - http://jibbering.com/faq/
    Jim Ley, Feb 16, 2004
    #4
  5. John Dalberg

    John Dalberg Guest

    On Mon, 16 Feb 2004 18:31:52 GMT, Jim Ley wrote:

    > On Sun, 15 Feb 2004 20:48:14 -0800, John Dalberg <>
    > wrote:
    >
    >> [12 quoted line(s) supressed]

    >
    > It does for me....
    >
    > Perhaps you should provide an example which demonstrates your problem.
    >
    > astyle is of course perhaps a more normal recommendation, but it's a
    > little pointless when you already have something that does the job.
    >

    Here's one:

    var cspbDetectedBrowser='IE5DHTML';var cspbVS='cspbPanelBarLoading';var
    cspbGD=new Object();var cspbFO=true;var cspbTL=0;var cspbTD=0;var
    cspbGDH=0;var cspbEIA=false;function
    cspbShowSection(){if(cspbEIA){return;}if(cspbVS=='cspbPanelBarLoading'){doc
    ument.all.cspbButtons.style.display='inline';document.all[cspbVS].style.dis
    play='none';cspbVS='';}var
    count=0;for(count=0;count<arguments.length;count++){var
    id=arguments[count];if(typeof(id)=='string'){id=cspbGetGroupIndex(id);}if(d
    ocument.all['cspbGroup'+id]){cspbGD=document.all['cspbGroup'+id];if(cspbGD.
    style.display=='inline'){document.all['cspbBtnCell'+id].className='cspbBtnC
    ell';if(navigator.platform!='MacPPC'){cspbGDH=cspbGD.offsetHeight;cspbEC(0)
    ;}else{cspbGD.style.display='none';}}else{document.all['cspbBtnCell'+id].cl
    assName='cspbBtnCellSlt';cspbGD.style.display='inline';if(!cspbFO&&navigato
    r.platform!='MacPPC'){cspbGDH=cspbGD.offsetHeight;cspbGD.style.height=1;csp
    bEO(cspbGDH);}}}}cspbFO=false;}function cspbGetGroupIndex(caption){var
    aCaptions=new
    Array();aCaptions[0]='Customers';aCaptions[1]='Orders';aCaptions[2]='Orders
    2';aCaptions[3]='About Us';var
    count=0;for(count=0;count<aCaptions.length;count++){if(aCaptions[count]==ca
    ption){return count;}}return 0;}function
    cspbHBT(id){document.all['cspbBtnText'+id].className='cspbBtnHvr';}function
    cspbDBT(id){document.all['cspbBtnText'+id].className='cspbBtn';}function
    cspbEO(l){var
    RES=3;cspbEIA=true;cspbTL=l;if(cspbTL==0){cspbGD.style.height=cspbGDH;cspbE
    IA=false;}else{if(cspbTL<=RES){cspbTD=1;}else{cspbTD=parseInt(cspbTL/RES);}
    if(cspbGDH!=cspbTL){cspbGD.style.height=cspbGDH-cspbTL;}setTimeout('cspbEO(
    cspbTL-cspbTD);',25);}}function cspbEC(l){var
    RES=6;cspbEIA=true;cspbTL=l;if(cspbGDH==cspbTL){cspbGD.style.display='none'
    ;cspbGD.style.height=cspbGDH;cspbEIA=false;}else{if((cspbGDH-cspbTL)<=RES){
    cspbTD=1;}else{cspbTD=parseInt((cspbGDH-cspbTL)/RES);}cspbGD.style.height=c
    spbGDH-cspbTL;setTimeout('cspbEC(cspbTL+cspbTD);',10);}}function
    cspbRefresh(){}function cspbSetHeight(px){}var cspbFSTID;var cspbFSIID;var
    cspbFSE=false;function
    cspbWSEH(){if(cspbFSE==true){clearTimeout(cspbFSTID);cspbFSTID=window.setTi
    meout('cspbSFSI();',200);}else
    if((navigator.userAgent.indexOf('MSIE')>-1)&&(navigator.platform!='MacPPC')
    ){cspbFSE=true;}}function
    cspbSFSI(){clearInterval(cspbFSIID);cspbFSIID=window.setInterval('cspbFS();
    ',1);}function cspbFS(){var
    scrollTop=parseInt(document.body.scrollTop)+0;var
    PBTop=parseInt(document.all['cspbPanelBar'].style.top);if(scrollTop==PBTop)
    {clearInterval(cspbFSIID);}else
    if(PBTop<scrollTop){if((scrollTop-PBTop)<=6){document.all['cspbPanelBar'].s
    tyle.top=PBTop+1;}else{document.all['cspbPanelBar'].style.top=parseInt(PBTo
    p+((scrollTop-PBTop)/6));}}else{if((PBTop-scrollTop)<=6){document.all['cspb
    PanelBar'].style.top=PBTop-1;}else{document.all['cspbPanelBar'].style.top=p
    arseInt(PBTop-((PBTop-scrollTop)/6));}}}function cspbASH(){var
    e=window.onscroll;if
    (typeof(e)=='function'){e=e.toString();e=e.substring(e.indexOf('{')+1,e.las
    tIndexOf('}'));var
    f=cspbWindowScroll.toString();f=f.substring(f.indexOf('{')+1,f.lastIndexOf(
    '}'));var sh=new
    Function(f+e);window.onscroll=sh;}else{window.onscroll=cspbWSEH;}}if(naviga
    tor.platform!='MacPPC'){this.attachEvent('onscroll',cspbWSEH);}
    John Dalberg, Feb 16, 2004
    #5
  6. John Dalberg <> writes:

    > Here's one:


    Not a very good example, because your news client broke the
    lines. That means that the code we see is not even syntactically
    correct Javascript, and we will have to work harder to just make it
    correct again, before we can start solving the problem.

    As for the method:
    Take that code, and put it into another file. Write these lines before
    the code:
    <script type="text/javascript">
    function foo(){
    and this after:
    };
    document.write("<pre>"+foo.toString()+"<\/pre>");
    </script>
    and load it into your browser as an HTML file. Use cut'n'paste to
    pick out the area between
    function foo() {
    and
    }

    In Opera it makes the blocks structure much clearer. In IE it doesn't
    help that much. Mozilla FireFox is by far the prettiest, with
    indentation and everything. Your code, as displayed by FireFox, is
    here (if the lines are broken, check your newsreader):

    ---
    var cspbDetectedBrowser = "IE5DHTML";
    var cspbVS = "cspbPanelBarLoading";
    var cspbGD = new Object();
    var cspbFO = true;
    var cspbTL = 0;
    var cspbTD = 0;
    var cspbGDH = 0;
    var cspbEIA = false;

    function cspbShowSection() {
    if (cspbEIA) {
    return;
    }
    if (cspbVS == "cspbPanelBarLoading") {
    document.all.cspbButtons.style.display = "inline";
    document.all[cspbVS].style.display = "none";
    cspbVS = "";
    }
    var count = 0;
    for (count = 0; count < arguments.length; count++) {
    var id = arguments[count];
    if (typeof (id) == "string") {
    id = cspbGetGroupIndex(id);
    }
    if (document.all["cspbGroup" + id]) {
    cspbGD = document.all["cspbGroup" + id];
    if (cspbGD.style.display == "inline") {
    document.all["cspbBtnCell" + id].className = "cspbBtnCell";
    if (navigator.platform != "MacPPC") {
    cspbGDH = cspbGD.offsetHeight;
    cspbEC(0);
    } else {
    cspbGD.style.display = "none";
    }
    } else {
    document.all["cspbBtnCell" + id].className = "cspbBtnCellSlt";
    cspbGD.style.display = "inline";
    if (!cspbFO && navigator.platform != "MacPPC") {
    cspbGDH = cspbGD.offsetHeight;
    cspbGD.style.height = 1;
    cspbEO(cspbGDH);
    }
    }
    }
    }
    cspbFO = false;
    }


    function cspbGetGroupIndex(caption) {
    var aCaptions = new Array();
    aCaptions[0] = "Customers";
    aCaptions[1] = "Orders";
    aCaptions[2] = "Orders2";
    aCaptions[3] = "About Us";
    var count = 0;
    for (count = 0; count < aCaptions.length; count++) {
    if (aCaptions[count] == caption) {
    return count;
    }
    }
    return 0;
    }


    function cspbHBT(id) {
    document.all["cspbBtnText" + id].className = "cspbBtnHvr";
    }


    function cspbDBT(id) {
    document.all["cspbBtnText" + id].className = "cspbBtn";
    }


    function cspbEO(l) {
    var RES = 3;
    cspbEIA = true;
    cspbTL = l;
    if (cspbTL == 0) {
    cspbGD.style.height = cspbGDH;
    cspbEIA = false;
    } else {
    if (cspbTL <= RES) {
    cspbTD = 1;
    } else {
    cspbTD = parseInt(cspbTL / RES);
    }
    if (cspbGDH != cspbTL) {
    cspbGD.style.height = cspbGDH - cspbTL;
    }
    setTimeout("cspbEO(cspbTL-cspbTD);", 25);
    }
    }


    function cspbEC(l) {
    var RES = 6;
    cspbEIA = true;
    cspbTL = l;
    if (cspbGDH == cspbTL) {
    cspbGD.style.display = "none";
    cspbGD.style.height = cspbGDH;
    cspbEIA = false;
    } else {
    if ((cspbGDH - cspbTL) <= RES) {
    cspbTD = 1;
    } else {
    cspbTD = parseInt((cspbGDH - cspbTL) / RES);
    }
    cspbGD.style.height = cspbGDH - cspbTL;
    setTimeout("cspbEC(cspbTL+cspbTD);", 10);
    }
    }


    function cspbRefresh() {
    }


    function cspbSetHeight(px) {
    }

    var cspbFSTID;
    var cspbFSIID;
    var cspbFSE = false;

    function cspbWSEH() {
    if (cspbFSE == true) {
    clearTimeout(cspbFSTID);
    cspbFSTID = window.setTimeout("cspbSFSI();", 200);
    } else {
    if ((navigator.userAgent.indexOf("MSIE") > -1) &&
    (navigator.platform != "MacPPC")) {
    cspbFSE = true;
    }
    }
    }


    function cspbSFSI() {
    clearInterval(cspbFSIID);
    cspbFSIID = window.setInterval("cspbFS();", 1);
    }


    function cspbFS() {
    var scrollTop = parseInt(document.body.scrollTop) + 0;
    var PBTop = parseInt(document.all.cspbPanelBar.style.top);
    if (scrollTop == PBTop) {
    clearInterval(cspbFSIID);
    } else {
    if (PBTop < scrollTop) {
    if ((scrollTop - PBTop) <= 6) {
    document.all.cspbPanelBar.style.top = PBTop + 1;
    } else {
    document.all.cspbPanelBar.style.top = parseInt(PBTop + ((scrollTop - PBTop) / 6));
    }
    } else {
    if ((PBTop - scrollTop) <= 6) {
    document.all.cspbPanelBar.style.top = PBTop - 1;
    } else {
    document.all.cspbPanelBar.style.top = parseInt(PBTop - ((PBTop - scrollTop) / 6));
    }
    }
    }
    }


    function cspbASH() {
    var e = window.onscroll;
    if (typeof (e) == "function") {
    e = e.toString();
    e = e.substring(e.indexOf("{") + 1, e.lastIndexOf("}"));
    var f = cspbWindowScroll.toString();
    f = f.substring(f.indexOf("{") + 1, f.lastIndexOf("}"));
    var sh = new Function(f + e);
    window.onscroll = sh;
    } else {
    window.onscroll = cspbWSEH;
    }
    }

    if (navigator.platform != "MacPPC") {
    this.attachEvent("onscroll", cspbWSEH);
    }
    ---

    Good luck
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Feb 16, 2004
    #6
  7. John Dalberg

    Randy Webb Guest

    Ira Baxter wrote:
    > "John Dalberg" <> wrote in message
    > news:zw30p2luaso4$.1hnbuil6c24rz$...
    >
    >>I have spent 1/2 hour looking for a Javascript formatter with no luck.
    >>I am using a tool that produces Javascript statements in one long string
    >>and it's hard to read.

    >
    >
    > Did you try Googling for "JavaScript Formatter"?
    > The first 20 entries or so like like perfectly good formatters to me.


    Your answer, of course, is not related to the SEMDesigns purchase of the
    #1 hit now would it? But whats the point in paying 50 dollars US for
    something that can be done for free? Sheesh.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
    Randy Webb, Feb 21, 2004
    #7
  8. Randy Webb <> writes:

    > Your answer, of course, is not related to the SEMDesigns purchase of
    > the #1 hit now would it?


    I'd say he was very nice about not pointing to it. :)

    > But whats the point in paying 50 dollars US for something that can
    > be done for free? Sheesh.


    Like this one:
    ---
    <textarea id="input" style="width:100%;height:10em">Enter code here</textarea>
    <input type="button" value="format"
    onclick="var inp=document.getElementById('input');
    var str=Function(inp.value).toString();
    str=str.substring(str.indexOf('{')+1);
    str=str.substring(0,str.lastIndexOf('}'));
    inp.value=str;">
    ---
    run in (preferably) Mozilla. Unless you like your comments ofcourse :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Feb 21, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A.M-SG

    Switching from XML formatter to Binary Formatter

    A.M-SG, Nov 21, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    324
    Steven Cheng[MSFT]
    Nov 22, 2005
  2. John Dalberg

    Javascript formatter?

    John Dalberg, Apr 1, 2005, in forum: Javascript
    Replies:
    9
    Views:
    95
    Dr John Stockton
    Aug 23, 2005
  3. Formatter using Javascript

    , Aug 18, 2005, in forum: Javascript
    Replies:
    2
    Views:
    79
    jsfromynr
    Aug 20, 2005
  4. mistral

    Javascript formatter tool

    mistral, Jun 9, 2006, in forum: Javascript
    Replies:
    3
    Views:
    153
    Lasse Reichstein Nielsen
    Jun 12, 2006
  5. Yoo

    online javascript formatter

    Yoo, Feb 12, 2007, in forum: Javascript
    Replies:
    0
    Views:
    243
Loading...

Share This Page