A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: CSS added with JS

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Location
    Lillestrøm, Norway
    Posts
    21

    Question CSS added with JS

    I have this script.

    Code:
    function addGlobalStyle(css) {
        var head, style;
        head = document.getElementsByTagName('head')[0];
        if (!head) { return; }
        style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css;
        head.appendChild(style);
    }
    
    addGlobalStyle('#column_left { background-color: #e4dab6; }');
    But instead of adding line for line with the CSS code I would like it to read a file called mystyle.css with all the nessesery css.
    It MUST be done with JS. How can this be done? Can anyone explane this for me?
    Last edited by Sinopa; 09-29-2008 at 03:47 PM.

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Why not just create a link tag and set the href attribute to your external sheet?
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    Senior Member
    Join Date
    Apr 2002
    Posts
    2,849
    If it has to be done with JS, try something like:

    Code:
    addGlobalStyle("@import url('mystyle.css');");

  4. #4
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    I think (historically at least) setting innerHTML on a style element was not well supported on a range of browsers. A better way to include your style sheet may be something like this,

    Code:
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = 'mystyle.css';
    link.type = 'text/css';
    document.getElementsByTagName('head')[0].appendChild(link);

  5. #5
    Retired SCORM Guru PAlexC's Avatar
    Join Date
    Nov 2000
    Location
    NJ
    Posts
    1,387
    Try the setClass method in SpryDomUtils.js

    http://labs.adobe.com/technologies/s...api/index.html

    Click on Spry Utilities and look under Utility Methods
    "What really bugs me is that my mom had the audacity to call Flash Kit a bunch of 'inept jack-asses'." - sk8Krog
    ...and now I have tape all over my face.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center