470,636 Members | 1,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Javascript Formatter?


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.
Jul 20 '05 #1
8 1814
On Sun, 15 Feb 2004 13:47:44 -0800, John Dalberg <jo*****@hotmail.com>
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/

Jul 20 '05 #2
On Sun, 15 Feb 2004 22:24:26 GMT, Jim Ley wrote:
On Sun, 15 Feb 2004 13:47:44 -0800, John Dalberg <jo*****@hotmail.com>
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.
Jul 20 '05 #3
On Sun, 15 Feb 2004 20:48:14 -0800, John Dalberg <jo*****@hotmail.com>
wrote:
On Sun, 15 Feb 2004 22:24:26 GMT, Jim Ley wrote:
On Sun, 15 Feb 2004 13:47:44 -0800, John Dalberg <jo*****@hotmail.com>
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/

Jul 20 '05 #4
On Mon, 16 Feb 2004 18:31:52 GMT, Jim Ley wrote:
On Sun, 15 Feb 2004 20:48:14 -0800, John Dalberg <jo*****@hotmail.com>
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';docum ent.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=cspb GD.offsetHeight;cspbEC(0)
;}else{cspbGD.style.display='none';}}else{document .all['cspbBtnCell'+id].cl
assName='cspbBtnCellSlt';cspbGD.style.display='inl ine';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.s tyle.height=cspbGDH;cspbE
IA=false;}else{if(cspbTL<=RES){cspbTD=1;}else{cspb TD=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){cs pbGD.style.display='none'
;cspbGD.style.height=cspbGDH;cspbEIA=false;}else{i f((cspbGDH-cspbTL)<=RES){
cspbTD=1;}else{cspbTD=parseInt((cspbGDH-cspbTL)/RES);}cspbGD.style.height=c
spbGDH-cspbTL;setTimeout('cspbEC(cspbTL+cspbTD);',10);}}f unction
cspbRefresh(){}function cspbSetHeight(px){}var cspbFSTID;var cspbFSIID;var
cspbFSE=false;function
cspbWSEH(){if(cspbFSE==true){clearTimeout(cspbFSTI D);cspbFSTID=window.setTi
meout('cspbSFSI();',200);}else
if((navigator.userAgent.indexOf('MSIE')>-1)&&(navigator.platform!='MacPPC')
){cspbFSE=true;}}function
cspbSFSI(){clearInterval(cspbFSIID);cspbFSIID=wind ow.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.substri ng(e.indexOf('{')+1,e.las
tIndexOf('}'));var
f=cspbWindowScroll.toString();f=f.substring(f.inde xOf('{')+1,f.lastIndexOf(
'}'));var sh=new
Function(f+e);window.onscroll=sh;}else{window.onsc roll=cspbWSEH;}}if(naviga
tor.platform!='MacPPC'){this.attachEvent('onscroll ',cspbWSEH);}
Jul 20 '05 #5
John Dalberg <jo*****@hotmail.com> 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

"John Dalberg" <jo*****@hotmail.com> wrote in message
news:zw******************************@40tude.net.. .

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.

-- IDB

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 20 '05 #7
Ira Baxter wrote:
"John Dalberg" <jo*****@hotmail.com> wrote in message
news:zw******************************@40tude.net.. .
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/

Jul 20 '05 #8
Randy Webb <hi************@aol.com> 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Alex Fitzpatrick | last post: by
4 posts views Thread by Patrick De Ridder | last post: by
4 posts views Thread by Carlitos | last post: by
2 posts views Thread by Nadav | last post: by
1 post views Thread by Edward Yang | last post: by
3 posts views Thread by mistral | last post: by
6 posts views Thread by Melih Onvural | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.