13

Hello Guys,

I was just trying to create a plugin, and I needed it to be user-friendly so I want to append the <link/> tag on the head portion of the users page as my plugin is loaded. It works fine with all the other browsers (not sure of IE9, IE7 & IE6) but doesn't work for IE8! I don't what is getting wrong with my plugin, so I just created a sample page and faced the similar problem! Here is my test page HTML+jQuery Code ------

<html>
       <head>
              <script src="../jquery-1.6.min.js"></script>
              <script>
                       $(document).ready(function () {
                          $(document.head).append('<link rel="stylesheet" type="text/css" href="style.css" />');
                       });
              </script>
       </head>
       <body>
              <h1>Text!</h1>
       </body>
</html>

And here is my CSS code -----

body {
 background:#ddd; 
}
h1 {
 color: #789; 
}

So can anyone tell me where I'm going wrong or is this the problem of lifetime?

THANKS IN ADVANCE


After seeing the first comment and going to link supplied, I just created this sample code and found something amazing! See this ~~~

<html>
       <head>
              <script src="../jquery-1.6.min.js"></script>
              <script>
                       $(document).ready(function () {
                          if (document.getElementsByTagName('head')[0] === document.head) {
                           $("head").append('<link rel="stylesheet" type="text/css" href="style.css" />');
                          }else {
                           alert('This doesn\'t supports head appending!');
                          }
                       });
              </script>
       </head>
       <body>
              <h1>Text!</h1>
       </body>
</html>

On executing this page with my IE8 browser I get the message that

This doesn't supports head appending!

Well I don't what is wrong with my browser or is this the fault of IE8?


Yahel
  • 37,023
  • 22
  • 103
  • 153
Jack Billy
  • 7,141
  • 6
  • 27
  • 38

2 Answers2

27
if (document.createStyleSheet)
{
    document.createStyleSheet("style.css");
}
else
{
    $("head")
       .append('<link rel="stylesheet" type="text/css" href="style.css" />'); 
}
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
5

You're using document.head to access the head. document.head is only a recent addition to the DOM, as part of HTML5. So, it's not universally supported.

If you want to use it, you should add a shim before calling it:

document.head = document.head || document.getElementsByTagName('head')[0];

Otherwise, you can just append directly to the <head> tag (which is recommended, since that's half the point of using jQuery's syntactic sugar):

$("head").append(...);
Yahel
  • 37,023
  • 22
  • 103
  • 153
  • I know but it is not working, see my newest Edit to my question. IE8 doesn't supporting appending tags in `head` section!? – Jack Billy May 21 '11 at 05:19
  • ...your alert merely proves that document.head isn't supported, as I said, not that you can't append to the head via JavaScript. If you add the shim, it should work. `document.getElementsByTagName('head')[0]` has universal support. – Yahel May 21 '11 at 05:35
  • Then how to append `link` tag on it? Please help me out. – Jack Billy May 21 '11 at 05:39
  • @Jack Billy: If you are OK with vanilla JavaScript, I have a code snippet. But I guess you know how to do it with js. – ace May 21 '11 at 05:52
  • Please provide everything you got to answer my question ~~ ace – Jack Billy May 21 '11 at 06:18