[Javascript] SAFARI javascript major problem

Roger Roelofs rer at datacompusa.com
Wed Jul 16 07:52:53 CDT 2008


Troy,

On Jul 15, 2008, at 4:30 AM, Troy III Ajnej wrote:

> The:
>    alert(document.styleSheets[0].rules[0].selectorText);
> will not work inside the script body.
> -Of course it will work, if, and only if there are no external  
> stylesheets
> presently linked to the document, but that's pointless & useless  
> equally.

Sorry I took so long to get back to you.  I just got back from  
vacation and I am still wading through the backlog.

Thanks for bringing up this question.  It has been educational for me.

if you link your stylesheet  like so: <link rel="stylesheet"  
href="test.css" type="text/css"> then  
alert(document.styleSheets[0].rules[0].selectorText will work just  
fine in Safari.  The properties for document.styleSheets[0].rules[0] are
	selectorText: body
	style: [object CSSStyleDeclaration]
	parentStyleSheet: [object CSSStyleSheet]
	cssText: body { }
	type: 1
	parentRule: null

However, if you _import_ your stylesheet like so:
	<style type="text/css">
	 @import url(test.css);
	</style>
the structure of the css is different and your first rule is an import  
rule which doesn't have selectorText.  If you display the properties  
of document.styleSheets[0].rules[0] you get

	href: test.css
	styleSheet: [object CSSStyleSheet]
	media: [object MediaList]
	parentStyleSheet: [object CSSStyleSheet]
	cssText: @import url("test.css") ;
	type: 3 parentRule: null

My guess is that ie and firefox are replacing the import rule with the  
contents of the imported file, while Safari is creating a hierarchy  
that matches the way you have specified your stylesheet in the DOM.   
You can get what you want in Safari by following the styleSheet object  
like so

document.styleSheets[0].rules[0].styleSheet.rules[0].selectorText

or you can change the html to only use linked stylesheets.

Sorry i don't have better news, but at least we now understand how  
Safari works in this case.

Roger
--
Roger Roelofs
Datacomp Appraisal Services
3215 Eaglecrest, NE
Grand Rapids, MI  49525




More information about the Javascript mailing list