dynamically apply a font family

A

Andrew Poulos

How do you apply a CSS font-family (say, Times, "New Century
Schoolbook", serif) to an object? This

obj.style.fontFamily = "Times, 'New Century Schoolbook', serif";

seems to ignore everything after Times.

Andrew Poulos
 
A

Andrew Poulos

Michael said:
seems to ignore everything after Times.
Well, it should, if "Times" is available.

Sorry, what I meant is that if the first font is unavailable then it
doesn't try any of the other fonts. It seems to only work with font
families that only consist of a single font name.

Andrew Poylos
 
L

-Lost

Andrew said:
Sorry, what I meant is that if the first font is unavailable then it
doesn't try any of the other fonts. It seems to only work with font
families that only consist of a single font name.

Hrmm... I see you are doing it the "CSS way." Did you try:

obj.style.fontFamily = "Times, New Century Schoolbook, serif"; ?
Andrew Poylos

If you have forgotten how to spell your own name I think you need to
back away from the computer for a while. ;)
 
A

Andrew Poulos

-Lost said:
Hrmm... I see you are doing it the "CSS way." Did you try:

obj.style.fontFamily = "Times, New Century Schoolbook, serif"; ?

I couldn't get it to work so I gave up and set a class instead.

Andrew Poulos
 
L

-Lost

Andrew said:
I couldn't get it to work so I gave up and set a class instead.

Um... OK. It *does* work though. Setting a class is more robust
anyway, though.
 
A

Andrew Poulos

-Lost said:
Um... OK. It *does* work though. Setting a class is more robust
anyway, though.
I tested it again (with FF 2 and IE 6) and multi-word font names get
ignored if I do either:
obj.style.fontFamily = "Timeees, 'New Century Schoolbook', serif";
or
obj.style.fontFamily = "Timeees, New Century Schoolbook, serif";

I named the first font intentionally wrong to see if 'New Century
Schoolbook' appears, and it doesn't. I get the default serif font.

Andrew Poulos
 
B

Ben Amada

Andrew said:
I tested it again (with FF 2 and IE 6) and multi-word font names get
ignored if I do either:
obj.style.fontFamily = "Timeees, 'New Century Schoolbook', serif";
or
obj.style.fontFamily = "Timeees, New Century Schoolbook, serif";

I named the first font intentionally wrong to see if 'New Century
Schoolbook' appears, and it doesn't. I get the default serif font.

Are you sure that the 'New Century Schoolbook' font is installed on your
computer? Without JS, does setting the style of an element to New Century
Schoolbook work?

I tried using JS to swap between the two list of font families:

"Pizza, 'New Century Schoolbook', 'Times New Roman', serif"
- and -
"Pizza, 'Comic Sans MS', 'New Century Schoolbook', Arial, serif"

When setting the element's fontFamily property to the top line above, I
would get Times New Roman and when setting it to the bottom line above, I
would get Comic Sans. I tried this in both Firefox 2 and IE7 with the same
results.

Ben
 
L

-Lost

Ben said:
Are you sure that the 'New Century Schoolbook' font is installed on your
computer? Without JS, does setting the style of an element to New Century
Schoolbook work?

I tried using JS to swap between the two list of font families:

"Pizza, 'New Century Schoolbook', 'Times New Roman', serif"
- and -
"Pizza, 'Comic Sans MS', 'New Century Schoolbook', Arial, serif"

When setting the element's fontFamily property to the top line above, I
would get Times New Roman and when setting it to the bottom line above, I
would get Comic Sans. I tried this in both Firefox 2 and IE7 with the same
results.

Yeah, it does work. I would still omit trying to inner quote it.
 
L

-Lost

Andrew said:
I tested it again (with FF 2 and IE 6) and multi-word font names get
ignored if I do either:
obj.style.fontFamily = "Timeees, 'New Century Schoolbook', serif";
or
obj.style.fontFamily = "Timeees, New Century Schoolbook, serif";

I named the first font intentionally wrong to see if 'New Century
Schoolbook' appears, and it doesn't. I get the default serif font.

Then I would have to guess that font does not exist on your system.
There is one known, it *DOES* work, so your error obviously lies
elsewhere (like perhaps, you not specifying the font name correctly).

I often find it funny (or sad) when people say, "Oh, I couldn't get it
to work." and then dismiss it. I would be more concerned about why
something so simple doesn't work.

I mean, you're not using Lynx to run this script are you?
 
A

Andrew Poulos

-Lost said:
Then I would have to guess that font does not exist on your system.
There is one known, it *DOES* work, so your error obviously lies
elsewhere (like perhaps, you not specifying the font name correctly).
You're right it does work without the single quotes. I was working on a
different computer and stupidly didn't check the installed fonts.

Andrew Poulos
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top