CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Sep 2007
    Posts
    28

    innerHTML in head

    can I do this validly?

    <script....>
    var headtag = document.getElementsByTagName(head);
    headtag.innerHTML += '<style>...</style>';
    </script>

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: innerHTML in head

    Not really. To my knowledge, once the page is loaded the CSS styles are set. You can change classes, but not add different styles.

    Plus, you have an error in that code anyway. The command is getElementsByTagName(). Notice that it is plural. It returns an array of all tags that match your specification.

    Code:
    var headtag = document.getElementsByTagName('head').item(0);
    What exactly are you hoping to accomplish? Maybe we can give you a better work-around.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: innerHTML in head

    If you're injecting CSS, here's a cross-browser (not thoroughly tested, though) way to do it:
    Code:
    if (document.createStyleSheet) {
        var style = document.createStyleSheet();
        style.addRule("div.content", "display: none;");
    } else {
        var head = document.getElementsByTagName("head")[0];
        if (head) {
            var style = document.createElement("style");
            style.setAttribute("type", "text/css");
            style.appendChild(document.createTextNode("div.content { display: none; }"));
            head.appendChild(style);
        }
    }
    I used the above code to hide elements in a script that was located in the <head> tag (at which point no elements are programmatically accessible.)

  4. #4
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    ill be adding LOTS of stlyes to the head upon request of the function, the stlye names will be built from variables from the rest of the script, does ur suggestion still work?

    And another quick one, isnt my method also cross bwoser? any down sides? lol

    thanks a lot

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: innerHTML in head

    The above code will work for you. It creates a new <style> tag and attaches it to the <head> tag. There should be no cross-browser issues.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    awesome guys, thanks a lot, but to be clear, my script idea

    <script....>
    var headtag = document.getElementsByTagName(head);
    headtag.innerHTML += '<style>...</style>';
    </script>


    accomplishes the same thing no?

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: innerHTML in head

    Quote Originally Posted by Kovo88
    accomplishes the same thing no?
    As I stated...not as it's written it won't.

    The command is getElementsByTagName(). Notice that it is plural. It returns an array of all tags that match your specification.

    Code:
    var headtag = document.getElementsByTagName('head').item(0);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    oh ok. because i want the script to write the style tags in the <head> tag, which is what I thought my script idea did, and of course, on one page, there is only one head tag.

    thanks

  9. #9
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    sorry peej, i just saw ur first reply :P

    ok basically, i want my script to add styles on the fly after the page has loaded, so that related elements on the page would aquire the new styles.

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: innerHTML in head

    The smartest/best way to do it would be to use Andreas' method.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    so if i wanted
    Code:
    if (document.createStyleSheet) {
        var style = document.createStyleSheet();
        style.addRule("div.content", "display: none; width: 500px;");
    } else {
        var head = document.getElementsByTagName("head")[0];
        if (head) {
            var style = document.createElement("style");
            style.setAttribute("type", "text/css");
            style.appendChild(document.createTextNode("div.content { display: none; } div.content { display: none; } div.content { display: none; } div.content { display: none; } div.content { display: none; }"));
            head.appendChild(style);
        }
    }
    just in theory of course

    paying attention to 3rd line and 3rd to last line
    Last edited by Kovo88; September 22nd, 2007 at 09:58 PM.

  12. #12
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    right?

  13. #13
    Join Date
    May 2002
    Posts
    10,943

    Re: innerHTML in head

    Yes. But I hope you don't really plan to repeat div.content { display: none; } so many times. You only need it once.

    Please remember to use code tags.

    &#91;code&#93;
    code goes here
    &#91;/code&#93;
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  14. #14
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    of course not!!! i said in theory, i just didnt want to create dummy css

  15. #15
    Join Date
    Sep 2007
    Posts
    28

    Re: innerHTML in head

    Code:
    var headID = document.getElementsByTagName("head")[0];         
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.appendChild(document.createTextNode("function add" + window_number + "Script() { var my" + window_number + "Fx = new Fx.Style(\"window_" + window_number + "_wone\", \'opacity\').start(0,1);"));
    
    newScript.appendChild(document.createTextNode("$(\"window_" + window_number + "_wone\").makeResizable({ handle:$(\"window_" + window_number + "_botright\"), container:$(\"window_" + window_number + "_wone\").getParent()});"));
    
    newScript.appendChild(document.createTextNode("$(\"window_" + window_number + "_inner\").makeResizable({ handle:$(\"window_" + window_number + "_botright\"), container:$(\"window_" + window_number + "_wone\").getParent()});"));
    
    newScript.appendChild(document.createTextNode("$(\"window_" + window_number + "_wone\").makeDraggable({ handle:$(\"window_" + window_number + "_handle\"), container:$(\"window_" + window_number + "_wone\").getParent()});"));
    
    newScript.appendChild(document.createTextNode("document.getElementById(\"window_" + window_number + "_wone\").style.height = \"257px\";}"));
    headID.appendChild(newScript);



    With that code, for some reason, if I call it in a function, say twice... for some reason, the effected elements on the page do not aquire the effects of that script.. Is there a way to parase the script once it is injected so that other functions can call it etc...?

    using eval() maybe?

Page 1 of 2 12 LastLast

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

Featured