Closed Bug 111943 Opened 24 years ago Closed 19 years ago

document.styleSheets not updated when a link element is appended to the head element

Categories

(Core :: DOM: CSS Object Model, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: martin.honnen, Unassigned)

References

Details

Attachments

(3 files)

I check document.styleSheets.length after I create and insert a link element and it always comes up one too low, that is after the first style sheet is added document.styleSheets.length is 0, then when the second style sheet is added document.styleSheets.length is 1. Although it seems the document is reloaded when the link element is inserted the body onload event doesn't fire. Thus there is no way for the scripter to know when the style sheet is accessible for scripting. IE6 doesn't have this problem, when the link element is added the document.styleSheets.length is updated immediately. Here is a test page <html> <head> <title>style sheet addition</title> </head> <body onload="alert(event.type);"> <input type="button" value="append style sheet green" onclick="var link = document.createElement('link'); link.rel = 'stylesheet'; link.href = 'test20011121.css'; link.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(link); document.body.appendChild(document.createTextNode('document.styleSheets.length: ' + document.styleSheets.length)); document.body.appendChild(document.createElement('br')); " /> <input type="button" value="append style sheet red" onclick="var link = document.createElement('link'); link.rel = 'stylesheet'; link.href = 'test20011126.css'; link.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(link); document.body.appendChild(document.createTextNode('document.styleSheets.length: ' + document.styleSheets.length)); document.body.appendChild(document.createElement('br'));" /><br /> </body> </html>
I think the difference between IE and Mozilla is that IE blocks script execution till the style sheet is loaded while Mozilla seems to load the style sheet asynchronously. For a script it seems better to load the style sheet synchronously, or we need a load event firing for the link element.
> Although it seems the document is reloaded when the link element is inserted > the body onload event doesn't fire. That's because the document isn't reloaded. We just re-resolve the style rules throughout the document. > Thus there is no way for the scripter to know when the style sheet is > accessible for scripting. Hmm, yes... that's true... I think the solution here is to propose to the DOM HTML WG a 'isLoaded' property or similar on the HTMLLinkElement interface. We do update document.styleSheets after it has finished loaded, so I don't think this is necessarily a bug.... What does MSIE do if you have something like this: var link = document.createElement('link'); link.rel = 'stylesheet'; link.href = 'someReallyLongTextFile.txt'; link.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(link); ...where 'someReallyLongTextFile.txt' contains no CSS? If it increments the length of document.styleSheets right away before finding out if the link really does contain a stylesheet, then I would consider that a bug in IE...
Why should the stylesheet array not include unrecognised or missing stylesheets?
Mass-reassigning bugs to dom_bugs@netscape.com
Assignee: jst → dom_bugs
The stylesheet array now includes missing and unrecognized sheets (so this bug, as stated, no longer exists). The new sheet _will_ however throw exceptions on most attempts to script it. I'll be adding onload events to sheets in the 1.4-1.5 timeframe. See bug 185236.
Depends on: 185236
So this should be marked fixed?
Yeah, probably.
Status: NEW → RESOLVED
Closed: 19 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: