Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how do I get the style sheets (or style object) of the wholedocument?

Reply
Thread Tools

how do I get the style sheets (or style object) of the wholedocument?

 
 
Jake Barnes
Guest
Posts: n/a
 
      04-10-2009

I know I can do this:

document.getElementById("id").style.property="valu e"

but how can I find all the styles that have been loaded?

Onload, I want to loop through every rule defined in the style sheets
of a page, and find every hover pseudo class, and preload all the
images defined therein.
 
Reply With Quote
 
 
 
 
Garrett Smith
Guest
Posts: n/a
 
      04-10-2009
Jake Barnes wrote:
> I know I can do this:
>
> document.getElementById("id").style.property="valu e"
>
> but how can I find all the styles that have been loaded?
>
> Onload, I want to loop through every rule defined in the style sheets
> of a page, and find every hover pseudo class, and preload all the
> images defined therein.


Possible, but if the point is to speed things up, doing that would have
the opposite effect.

Garrett
 
Reply With Quote
 
 
 
 
rf
Guest
Posts: n/a
 
      04-10-2009
Jake Barnes wrote:
> I know I can do this:
>
> document.getElementById("id").style.property="valu e"
>
> but how can I find all the styles that have been loaded?
>
> Onload, I want to loop through every rule defined in the style sheets
> of a page, and find every hover pseudo class, and preload all the
> images defined therein.


http://wellstyled.com/css-nopreload-rollovers.html

Be sure to follow the "example" link.

No preloading required. No messy traversal of stylesheets required.

--
Richard.


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 3:02*pm, Jake Barnes <lkrub...@geocities.com> wrote:
> I know I can do this:
>
> document.getElementById("id").style.property="valu e"
>
> but how can I find all the styles that have been loaded?


Play with document.styleSheets, which belongs to interface
DocmentStyle:

<URL: http://www.w3.org/TR/DOM-Level-2-Sty...le-styleSheets
>



You can use it to get each style sheet:

<URL: http://www.w3.org/TR/DOM-Level-2-Sty...ets-StyleSheet
>


From there you go over all the style rules. There are differences
between browsers, search the archives.


> Onload, I want to loop through every rule defined in the style sheets
> of a page, and find every hover pseudo class, and preload all the
> images defined therein.


Probably a waste of time, there are better strategies (see other
replies).


--
Rob
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      04-10-2009
Am Fri, 10 Apr 2009 06:09:22 +0000 schrieb rf:

> Jake Barnes wrote:
>> I know I can do this:
>>
>> document.getElementById("id").style.property="valu e"
>>
>> but how can I find all the styles that have been loaded?
>>
>> Onload, I want to loop through every rule defined in the style sheets
>> of a page, and find every hover pseudo class, and preload all the
>> images defined therein.

>
> http://wellstyled.com/css-nopreload-rollovers.html
>
> Be sure to follow the "example" link.
>
> No preloading required. No messy traversal of stylesheets required.



The (IMO) huge problem with this solution: The elements require a fixed
(pixel-)size (depending on the background image either height or width),
which makes flexible layouts impossible. Just try to increase the font-
size here:
http://wellstyled.com/files/css-nopr...s/example.html

Gregor


--
http://www.gregorkofler.com
http://web.gregorkofler.com - vxJS, a JS lib in progress
 
Reply With Quote
 
Lawrence Krubner
Guest
Posts: n/a
 
      04-12-2009
On Apr 10, 2:09*am, "rf" <r...@z.invalid> wrote:
> JakeBarnes wrote:
> > I know I can do this:

>
> > document.getElementById("id").style.property="valu e"

>
> > but how can I find all the styles that have been loaded?

>
> > Onload, I want to loop through every rule defined in the style sheets
> > of a page, and find every hover pseudo class, and preload all the
> > images defined therein.

>
> http://wellstyled.com/css-nopreload-rollovers.html
>
> Be sure to follow the "example" link.
>
> No preloading required. No messy traversal of stylesheets required.



No, that doesn't work. The designer working on this project used that
trick where it was appropriate (nav bar, some rollovers) but we are
now dealing with situations where that trick can not easily be used.
In particular, some items of clothing have the price appear on
rollover, the rollover is an image that has text added dynamically by
PHP, and she's using the replace-the-whole-damn-image approach to deal
with the problem IE 6 has with transparent pngs.

But thanks.





 
Reply With Quote
 
Lawrence Krubner
Guest
Posts: n/a
 
      04-12-2009
I seem unable to get the data I want, at least on FireFox, where I'm
currently testing the script. This is the code that I currently have
(it runs onload):

var styleSheets = document.styleSheets;
var stringOfAllStyleRules = "";
for (var i=0; i < styleSheets.length; i++) {
var thisStyleSheet = styleSheets[i];
if (thisStyleSheet.cssRules) {
var listOfStyleSheetRules = thisStyleSheet.cssRules;
}
if (thisStyleSheet.rules) {
var listOfStyleSheetRules = thisStyleSheet.rules;
}

if (typeof listOfStyleSheetRules == "undefined") {
window.status = "No style rules could be found";
} else {
for (var r=0; r < listOfStyleSheetRules.length; r++) {
var thisRule = listOfStyleSheetRules[r];
if (typeof thisRule["style"] != "undefined") {
var styleObject = thisRule["style"];
var backgroundImageUrl = styleObject["backgroundImage"];
if (backgroundImageUrl != "") {
stringOfAllStyleRules += backgroundImageUrl;
stringOfAllStyleRules += "<br /> \n";
}
}
}
}
}
if (stringOfAllStyleRules) document.getElementById("recently-viewed-
items").innerHTML = stringOfAllStyleRules;


That final line, where I write the data to recently-viewed-items, is
just so that I can see what is going on. Once I've got the images that
I want, I'll do something more interesting.

However, I'm not getting the URLs that I want. This is all I get:


url(Simplicity%20Home/bg-body.jpg)
none
none
none
none
url(../img/icon-envelope-on.gif)
url(../img/icon-envelope.gif)
url(../img/icon-mobile-on.gif)
url(../img/icon-mobile.gif)


Meanwhile, the style sheets have rules such as this:

li#patterns em, li#apparel em, li#tapes em, li#appliques em,
li#closures em, li#mend em, li#decor em, li#sewing em, li#quilting em,
li#ribbon em, li#crochet em, li#kits em, li#products em{
background: url(Simplicity Home/bg-nav.png) no-repeat;
cursor: pointer;
}
/*Patterns*/
body.patterns li#patterns em{
background: url(Simplicity Home/bg-nav-patterns.png) no-repeat;
}
body.patterns li#patterns:hover em{
background: url(Simplicity Home/bg-nav-patterns.png) no-repeat;
background-position: 0 -46px;
}


None of those background images are being discovered by my script.
What am I doing wrong?
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Firefox not recognizing style sheets Elyse Sommer Firefox 6 06-01-2005 01:30 AM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM
export to Excel - multiple sheets & renaming sheets Carl Corcoran ASP General 1 11-12-2003 07:28 PM
Re: Using external style sheets Kevin Spencer ASP .Net 0 07-09-2003 06:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57