Topic: Is there a way to getComputedStyle from an external stylesheet in Javascript? |
|
|---|---|
| Author | Thread |
|
Paranoid (IV) Inmate From: Johnstown, PA |
posted 11-07-2008 14:52
My co-worker is writing some js code which retrieves a style from an external stylesheet for use in a menu. The problem he is having is that when he attempts to retrieve the css attributes from a style class in the external stylesheet, he will retrieve the default values rather than those defined in the stylesheet. code: function GetMenuStyles(normID,hoverID)
{
var nColor,nTextColor,nFontSize;
var hColor,hTextColor,hFontSize;
//normal
var ref=document.getElementById(normID);
if(ref)
{
if (ref.currentStyle)
{
nColor=ref.currentStyle.backgroundColor;
nTextColor=ref.currentStyle.color;
nFontSize=ref.currentStyle.fontSize;
}
else if(document.defaultView.getComputedStyle)
{
var elementStyle=document.defaultView.getComputedStyle(ref,null);
if (elementStyle)
{
nColor=elementStyle.getPropertyValue("background-color");
nTextColor=elementStyle.getPropertyValue("color");
nFontSize=elementStyle.getPropertyValue("font-size");
}
}
}
//Hover
var hID;
if(hoverID) hID=hoverID;
else hID=normID;
ref=document.getElementById(hID);
if(ref)
{
if (ref.currentStyle)
{
hColor=ref.currentStyle.backgroundColor;
hTextColor=ref.currentStyle.color;
hFontSize=ref.currentStyle.fontSize;
}
else if(document.defaultView.getComputedStyle)
{
var elementStyle=document.defaultView.getComputedStyle(ref,null);
if (elementStyle)
{
hColor=elementStyle.getPropertyValue("background-color");
hTextColor=elementStyle.getPropertyValue("color");
hFontSize=elementStyle.getPropertyValue("font-size");
}
}
}
//validate that stuff is defined, provide defaults
if(!nColor) nColor='#00ff00';
if(!nTextColor) nTextColor='#000000';
if(!nFontSize) nFontSize='12px';
if(!hColor) hColor='#00ff00';
if(!hTextColor) hTextColor='#000000';
if(!hFontSize) hFontSize='12px';
//convert the style colors to hex without the #
nColor=convertColor(nColor);
nTextColor=convertColor(nTextColor);
hColor=convertColor(hColor);
hTextColor=convertColor(hTextColor);
var rtrnObject= { normal: { backgroundColor: nColor, textColor: nTextColor, fontSize: nFontSize },
hover: { backgroundColor: hColor, textColor: hTextColor, fontSize: hFontSize }
};
return rtrnObject;
/*
var normColor=getClassColors(normClass);
var hoverColor=getClassColors(hoverClass);
return { normal: normColor, hover: hoverColor };
*/
}
code: .linkPrimarySiteNav
{
background-color: #0080FF;
color: #FFFFFF;
text-decoration: none;
font-size: 16px;
font-weight: bold;
}
.linkPrimarySiteNav_hover,.linkPrimarySiteNav:hover
{
background-color: #30b0FF;
color: #00FF00;
text-decoration: none;
font-size: 12px
}
|
|
Paranoid (IV) Inmate From: Norway |
posted 11-07-2008 15:58
|
|
Paranoid (IV) Inmate From: Johnstown, PA |
posted 11-07-2008 16:22
Yes poi, |
|
Paranoid (IV) Inmate From: Florida |
posted 11-07-2008 18:13
You can always show him instead of telling him, but |
|
Bipolar (III) Inmate From: |
posted 05-31-2011 11:10
Edit TP: spam removed
|