Retrieving font family

J

Jeremy

I'm writing a script for image replacement. The idea is that after the
page loads, certain elements will have their text replaced with an image
generated by a server-side script. This works fine.

The problem I'm having is this: I want to use CSS to define the salient
styling parameters and have the javascript retrieve those parameters and
pass them on to the server-side script that generates the image. This
is working fine for color, background-color, and font-size.

However, I'm having trouble with font-family. I want to send the entire
font-family string to the server-side script so that it can walk down
the list of fonts until it finds one that is present (much like a
user-agent does). See the end of the message for the code I'm using to
get the style.


When I retrieve the computed style for font-family, I get the following
behavior:

IE and Mozilla return the comma-delimited list of fonts that were
defined in the CSS. This is what I want.

Safari (and presumably Konq) returns only the first font in the CSS
list. This is okay, but not ideal (it assumes that the server always
has the first choice font installed).

Opera returns only the font that was applied to the element. This is
bad (although I can imagine it being useful for other applications).


Does anyone have an idea of a better way to get the entire list of
specified font possibilities for an element?


Code for my getStyle function (adapted from some Quirksmode code with a
little modification).

function getStyle(el,styleProp)
{
var x = el;
var y = "";
if (x.currentStyle)
{

//this part would be better done with a regex callback,
//but those are not widely supported AFAIK

var iePropWords = styleProp.split("-");
var ieProp = "";
for(var i = 0; i < iePropWords.length; i++)
{
if(i > 0)
{
ieProp += iePropWords.substring(0, 1).toUpperCase();
ieProp += iePropWords.substring(1);
}
else
ieProp += iePropWords;

}

y = x.currentStyle[ieProp];
}
else if (document.defaultView && document.defaultView.getComputedStyle)
y =
document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);

return y;

}
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top