Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-28-2003, 07:25 PM   PM User | #1
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
Smile printf()

Any of you old C wolves ever feel nostalgic about good, old, trusted printf()?
I did, and since when it comes to JavaScript I write first and Google later, here is what I came up with:
www.vladdy.net/Demos/printf.html

Integer length modifiers and pointer types are not supported in my emulation for obvious reasons.

Bugs are quite possible - bug reports are quite welcome.

WA: I thought the script is too long to post inline. If you think different, I would be happy to follow the category guidelines and correct my mistake.
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

Last edited by Vladdy; 07-28-2003 at 07:31 PM..
Vladdy is offline   Reply With Quote
Old 07-28-2003, 07:40 PM   PM User | #2
MotherNatrsSon
Senior Coder

 
Join Date: Mar 2003
Location: OHIO
Posts: 1,438
Thanks: 1
Thanked 0 Times in 0 Posts
MotherNatrsSon is an unknown quantity at this point
It does not work for me in Netscape 7 on a Mac. I get this error from the javascript console:

Error: syntax error
Source Code:
printf('',);

MNS
__________________
[size=1]"If you want to be "in the biz" you are going to have to roll with the changes or get out, basically."
MotherNatrsSon is offline   Reply With Quote
Old 07-28-2003, 07:46 PM   PM User | #3
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
You need to add some arguments:
printf('Integer: %i',23)

printf('',) will sure cause a syntax error.
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"
Vladdy is offline   Reply With Quote
Old 07-28-2003, 07:55 PM   PM User | #4
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
Ahh,
reminds me of my 'C' programming days.

Seems to work. Tried the hex "%09x" and decimal "%09d" and it worked ok untill I did

printf("images/img%03d.gif", 1); => images/img001.gif
printf("images/img%03d.gif", 2); => images/img002.gif
printf("images/img%03d.gif", 3); => images/img003.gif

but then

printf("images/img%03d.gif", 0); => images/img.gif

(same for %03x)
RoyW is offline   Reply With Quote
Old 07-28-2003, 09:11 PM   PM User | #5
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
Nice catch, Roy!
Fixed.
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"
Vladdy is offline   Reply With Quote
Old 07-29-2003, 02:39 AM   PM User | #6
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
I can already see some uses for this script.

instead of
Code:
document.write"<div style='position:absolute;width:"+w+";height:"+h+"'></div>");
It would become
Code:
var w=200;
var h=100;
printf("<div style='position:absolute;width:%d;height%d'></div>", w,h);
Output:
<div style='position:absolute;width:200;height100'></div>


Which seems a lot easier to read.

And I tried this and it worked

Code:
var red=128;
var green=255;
var blue=127;

if(moz)
    printf("font-color: rgb(%d %d %d); ", red, green, blue);
else
    printf("font-color:#%2x%2x%2x;", red, green, blue);
Output:
font-color: rgb(128 255 127);
or
font-color:#80ff7f;

A very nice utility function, thanks for sharing.
(Can you now do scanf?
scanf(elem.style.fontColor, "#%2x%2x%2x", &r, &g, &b)
)
RoyW is offline   Reply With Quote
Old 07-29-2003, 02:53 AM   PM User | #7
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
Still playing...

Could be very usefull for those wanting to format currency. Try typing this into the test line
Code:
str="";for(i=0 ; i<10 ; i++) {money=Math.random()*400;str += (printf("$ %6.02f", money)  + "\n" );}str;
RoyW is offline   Reply With Quote
Old 07-29-2003, 05:26 PM   PM User | #8
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
I'm glad you found it useful.
as far as scanf goes... cant do &r
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"
Vladdy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:30 PM.


Advertisement
Log in to turn off these ads.