Javascript Formatter?

J

John Dalberg

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

Jim Ley

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

Jim Ley

[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.
 
J

John Dalberg

[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);}
 
L

Lasse Reichstein Nielsen

John Dalberg said:
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);
}
 
R

Randy Webb

Ira said:
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.
 
L

Lasse Reichstein Nielsen

Randy Webb said:
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;">
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top