Photo from Flickr: sakaki/699723115 Photo by ukine @ flickr

Dear Opera guys…

published on
September 2nd, 2008
by Marko Mrdjenovic
Large bookcase #1, 3rd shelfImage by vjl via Flickr

Can you please tell me how to add external stylesheets with JavaScript? I’m trying to add them by creating a link element with a rel=”stylesheet”, type=”text/css” and a valid href and then appending this to the head element but it doesn’t seem to work. I’m quite sure my css files are good since when stripped down they only contain body {background-color:#ff0000;}.

It’d be great if you could post a solution because I really don’t want to spend another hour thinking about this – I already spent four and I got nowhere.

Reblog this post [with Zemanta]

Opinions

  1. posted on
    September 2nd, 2008
    by lojze

    what about import?

  2. posted on
    September 2nd, 2008
    by lojze

    oh… something ate my code lol

  3. posted on
    September 2nd, 2008
    by zproxy

    Hey,

    My ScriptCoreLib frameworks uses the following:

    1. add a new STYLE with content default /**/ to HEAD element
    2. call insertRule or addRule to specify selector
    3. access the .style member on the newly created rule to set values

    This might not work on older opera versions like the one i have on my Amino 110 IPTV…

    ScriptCoreLib is written in c# yet you might find it as a good reference:
    http://jsc.svn.sourceforge.net/viewvc/jsc/core/ScriptCoreLib/JavaScript/DOM/IStyle.cs?revision=733&view=markup

  4. posted on
    September 2nd, 2008
    by Marko

    Interesting approach. Ghastly, but interesting nevertheless.

  5. posted on
    September 2nd, 2008
    by Marko Mrdjenovic

    I’ve got an idea of my own – add a style and include a stylesheet with an import rule. Have to test it…

  6. posted on
    September 2nd, 2008
    by Marko Mrdjenovic

    I’ve been debugging this a bit – when I make a simple example it works flawlessly. So I went back to WordPress where the problem first occured and it still does. From what I gathered the link element is added to the head and is also present in the document.styleSheets array but its cssRules array breaks Opera when accessed, while href is returned normally.

    Even if analysis is postponed the stylesheet remained disabled and without the cssRules array. Weird.

  7. posted on
    September 2nd, 2008
    by Marko Mrdjenovic

    It seems the stylesheet doesn’t load only when I’m serving it from a local Apache server…

Express your opinion

My name is Marko Mrdjenovič and I’m a web developer and a manager from Ljubljana, Slovenia (N 46° 03.246,W 14° 30.265).

I like solving problems. I do that by writing code, managing projects and people. I like creating good experiences. And going to conferences. I work at Zemanta. I do freelance UX/front-end/back-end work, available from March.

Blog archive

Advertisments

Jobs

Come in, we're hiring

Full-time and freelance job opportunities available at Authentic Jobs:

  • Loading ...

Post a job and reach web professionals everywhere.