Forum Moderators: open

Message Too Old, No Replies

get variable from file?

         

sssweb

4:02 pm on Mar 9, 2006 (gmt 0)

10+ Year Member



Is there a way to get a variable from an external CSS file, and use it in a js script on the current page? Say my external CSS file looks like:

.class {
width: 100px;
}

So in a js script on the current page, I want to set:

var x = 100 [or whatever the CSS file says]

Bernard Marx

6:11 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Swap these ¦¦ for uncorrupted pipes

<html>
<head>
<style type="text/css">
#test
{
width: 100%;
}

.hello
{
color: green;
}
</style>
<script type="text/javascript">

function getCSSRule(selectorText, iSheet)
{
var sheets = document.styleSheets;
/* Stop, if unsupported(*) */
if(!sheets) return;
/* If no sheet specified, use last sheet */
var sheet = sheets[ isNaN(iSheet)? sheets.length-1 : iSheet];
/* Get rules collection ( W3C¦¦IE )
var rules = sheet.cssRules¦¦sheet.rules;
/* Search for selector with matching text*/
for(var k=-1, rule; rule=rules[++k];)
if(rule.selectorText == selectorText)
return rule;
}

var myRule = getCSSRule(".hello")

/*
Once reference to rule has been gained,
its style object can be treated in the same way
as an element style object.
- it is Read/Write
*/
if(myRule)
alert( myRule.style.color )
</script>

</head>
<body>

HELLO

</body>
</html>

(*)Opera doesn't support this yet.

sssweb

8:11 pm on Mar 9, 2006 (gmt 0)

10+ Year Member



I can't get that to work. Questions:

1) Does it access an EXTERNAL style sheet? That's what I need.

2) If so, is the style sheet supposed to be named 'styleSheets.css'?

3) Does it look for the style sheet in my root directory? That's what I need.

4) Any other customization I need to do (other than fixing the pipes)?

Bernard Marx

10:53 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Yes, any stylesheet. Here are 3:

<link rel="stylesheet" href="css/stuff.css" type="text/css">
<style type="text/css">
#test{color:red;}
</style>
<link rel="stylesheet" href="things.css" type="text/css">

That's document.styleSheets[0] ,[1] and [2]

2) Can be "called" anything, or be inline.
3) Any stylesheet
4) When you call the function, if you don't specify the index of the sheet required (source order), then the last sheet is searched. If there's only one sheet, that is the last, and will be searched.

Bernard Marx

11:31 pm on Mar 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh yes...

The function will only be able to detect the stylesheets that appear in the code above the execution call.

sssweb

1:58 am on Mar 10, 2006 (gmt 0)

10+ Year Member



Okay, I think I understand what you're saying, but I still can't get it to work. I added the link to my external stylesheet above the other style references (and above the script itself), and called it using:

var sheets = document.styleSheets[0];

It has no effect on the text (I know my CSS is okay because when I assign it directly, not through your script, it modifies the text).

Even when I copy your script as is, with the inline style last in line above the script, and no stylesheet index assigned, it doesn't work. Did you test it in IE?

I don't need to edit anything in these lines, do I?:

for(var k=-1, rule; rule=rules[++k];)
if(rule.selectorText == selectorText)

Bernard Marx

9:13 am on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I retested the code I posted, and found that I had forgotten to close one of the comments
(I shouldn't add things after testing)

 /* Get rules collection ( W3C¦¦IE ) [red]*/[/red]

After that amendment it works. If you're still having trouble, post the code you are using (but remove all the irrelevant stuff)

sssweb

5:04 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



Well, I thought that would've done it, but it's still no go. Here's what I'm using:

<html>
<head>

<link rel="stylesheet" href="/TEST/styletest.css" type="text/css" media="all">
<style type="text/css">
.hello
{ color: green; }
</style>

<script type="text/javascript">
function getCSSRule(selectorText, iSheet)
{
var sheets = document.styleSheets;
if(!sheets) return;
var sheet = sheets[ isNaN(iSheet)? sheets.length-1 : iSheet];
var rules = sheet.cssRules¦¦sheet.rules;
for(var k=-1, rule; rule=rules[++k];)
if(rule.selectorText == selectorText)
return rule;
}
var myRule = getCSSRule(".hello")
if(myRule)
alert( myRule.style.color )
</script>

</head>
<body>
HELLO
<br><br>
<font CLASS="hello">TEST</font>
</body>
</html>

I take it this is supposed to give an alert message indicating my CSS rule, correct? Is the 'HELLO' in <body> supposed to conform to the CSS too? I get neither. ('TEST' does conform to CSS class .hello)

I tested this using 3 different lines:

var sheets = document.styleSheets;
var sheets = document.styleSheets[0];
var sheets = document.styleSheets[1];

Bernard Marx

6:29 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have run the code you posted (correcting the ¦¦). It works in both IE & FF.

The call,

getCSSRule(".hello")
, should search in the last sheet - which is the <style> block. The function returns a reference to the "
.hello
" rule.

alert(myRule.style.color)
alerts: "green" (without quotes)

Is the 'HELLO' in <body> supposed to conform to the CSS too? I get neither. ('TEST' does conform to CSS class .hello)

As you have it, yes. However, the page doesn't need any actual body content at all. I suppose I put "HELLO" in there just for the sake of it.

I tested this using 3 different lines: ...

Only this one will work:

var sheets = document.styleSheets; 

The others will produce errors, unless the function's code is amended slightly.

I can't see why it's not working for you, but you could try some of your own investigation. The first step is always to drag an alert through the function to see if the function is being called, and, if it is, where it is tripping up.

Better, in fact, to start by putting an alert at the top of the script block (

alert(1)
) to see if any Javascript is being executed at all.

sssweb

8:36 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



All right, I got it -- for some reason, when I hit my IE refresh it wasn't updating the page; I had to go in and manually clear my cache. Leave it to Bill Gates....

I tweaked it a bit to fit my needs -- I want to set a variable = to the 'width' spec of CSS class 'images'. Here's what I did (seems to work, but let me know if you see something off):

[last part of the script, in place of the alert ]:

var myRule = getCSSRule(".images")
/* if(myRule) */
var setwidth = ( myRule.style.width )

Do I need 'if(myRule)'? Seems to work even when I comment it out.

Last thing -- I'm confused about specifying a stylesheet other than the default (last one). Where exactly do I indicate the index [0], [1], [2], etc.?

sssweb

8:43 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



[see previous post too]

Oh...how do I tweak the following to give a default value to my variable if it doesn't find the stylesheet?

var sheets = document.styleSheets;
if(!sheets) return;

Bernard Marx

9:37 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do I need 'if(myRule)'? Seems to work even when I comment it out.

It's just there to prevent errors. If you try to get a rule that doesn't exist..

myRule = getCSSRule("#idonot > .exist")

then this below will produce an error, because

myRule
has value
undefined
.

alert(myRule.style.color)

I'm confused about specifying a stylesheet other than the default (last one). Where exactly do I indicate the index [0], [1], [2], etc.?

myRule = getCSSRule(".hello", 0)

..Specifying the first stylesheet.

The function would be improved by an ability to search all the stylesheets (in reverse order) for the specified rule. I haven't got there yet.

Oh...how do I tweak the following to give a default value to my variable if it doesn't find the stylesheet?

var sheets = document.styleSheets;
if(!sheets) return;

Not sure what you mean there. The quoted code snippet is there, again, to prevent error. If the browser doesn't have a defined

document.styleSheets
collection (eg Opera) then the function will return undefined immediately (you can take action accordingly).

sssweb

10:24 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



For Opera, I was thinking I could insert something like the following to set a default value if it can't read the stylesheet:

if(!sheets)
var setwidth = 100
return;

But that doesn't work. So I just did this at the end:

var myRule = getCSSRule(".images", 1)
if(myRule)
var setwidth = ( myRule.style.width )
else
var setwidth = 100

Seems to work. I think that does it -- thanks very much for all your help.