Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

createStyleSheet error

Status
Not open for further replies.

cyrrhose

Programmer
Sep 22, 2003
5
CH
Hello,
I'm having an "unspecified error" when I refresh a page at the line where I add a style sheet to my document, after having removed all the styles and class attributes of the elements of the page, when I use :

document.createStyleSheet("user.css",0);

The first time this is executed ok, the second time it gives me an error with the very helpfull "unspecified error" error message.
Has anyone seen such problems using this method?
Thanks in advance.
 
Try leaving the index out - loading the stylesheet into the end of the stylesheets collection:
document.createStyleSheet("user.css");

See if that brings you any joy.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Hello,
I've tried, but it doesn't change the problem...
 
Are you sure it's that line that is causing you grief. In my experience, browsers rarely report the correct line number.

Try placing an alert("Got to here"); right before the line that the browser reports as faulty and see if you get the message.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
After having place some alerts before and after the line, I'm now sure it is the line that causes the error. But it's funny, if I place an alert before, the error message doesn't show! In my opinion it's something to do with loading time, the line must try to add a stylesheet before the page is completely loaded, and the alert gives some more time to load...
 
Could easily be the case, is the code being executed as it loads or in a function called by the body's onload event. If you call it as a function of the onload event you shouldnt be havingany issues as onload only fires once the page has finished loading.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
It is executed when it loads, maybe I have to put a timer or a test based on the page's status???
 
Put it inside a function:
[tt]
function doAddCSS(){
document.createStyleSheet("user.css");
}
[/tt]

Then call that function from the body tag as a handler for the onload event:
[tt]
<body onload=&quot;doAddCSS();&quot;>
[/tt]

That way, you will ensure that the code only runs when the page has finished loading, regardless of how long that takes.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Just try calling it onload like dwarfthrower suggested.

Code:
window.onload = function() {

    document.createStyleSheet(&quot;user.css&quot;);
}
 
mmh, that's how it is called, but you're right the function call refers to a second frame that maybe isn't loaded at that time, so I think I'll have to test the other windows' status. I think i'll find out using the dhtml reference... Thanks for all! ...and nice name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top