1

I'm making a zoom button to add and remove a CSS file and for some reason I can't seem to add it in IE 8

First I tried this

document.createStyleSheet('style/zoom.css');

given that the jquery solution

$("head").append($("<link my style sheet />"));

seems to only work in FF and IE9 via my testing

I checked around the overflow and found this solution

 $('#zoomlink').replaceWith($('<link>', {
        id: 'zoomlink',
        href: 'style/zoom.css',
        type: 'text/css',
        rel: 'stylesheet' 
 }));

But still no love to be found so then frustrated i found this

var $link = $('&lt;link&gt;');
    $('head').add($link);
    $link.attr({
      type: 'text/css',
      href: 'style/zoom.css',
      type: 'text/css',
      rel: 'stylesheet',
      media: 'screen'
});

which im not certain would ever work but then finally i decided it way time to simply post a question.

I'm still not certain on how to remove the style sheet later via javascript but I need to first determine how to add a new style sheet in IE 8.

3
  • $('&lt;link&gt;') should be $('<link>') Commented Sep 25, 2012 at 19:16
  • .add should be .append also. Commented Sep 25, 2012 at 19:29
  • well now it throws an error "expected identifier" in regards to $('<link>'); Commented Sep 25, 2012 at 20:13

2 Answers 2

4

Maybe this will help (Sorry, can't try it in IE8):

(function() {
    var s  = document.createElement('link');
    s.type = 'text/css';
    s.rel  = 'stylesheet';
    s.href = 'http://yourdomain.com/style.css';
    document.getElementsByTagName("head")[0].appendChild(s);
})();
Sign up to request clarification or add additional context in comments.

5 Comments

To remove this stylesheet later, use document.getElementsByTagName("head")[0].removeChild(s);
@ert3 Please try it out in JSFiddle: jsfiddle.net/ZT6nt. CSS property "font-size" must increase after click on "Add" and decrease after "Remove".
@IgorShastin: Sweet how you go that to work in jsfiddle. I tried and inspecting the DOM it just would not work but did work in a normal web app. I keep a copy of that for the settings on the fiddle. +1 nice one.
i should not have jumped to conclusions you sir are a life saver
YES, document.getElementsByTagName("head")[0].appendChild(fileref); fixed my IE8 error. Was using jQuery("head").append(fileref); which did not work properly. Thank you mr. @IgorShastin Any ideas why plain javascript worked indsted of jQuery here?
1

To remove it I would assume a simple remove() would work

$("#zoomlink").remove();

To add a new link using jQuery with this syntax:

$("head").append(unescape("%3Clink id='zoomlink' rel='stylesheet' type='text/css' href='style/zoom.css'%3E%3C/link%3E"));

or using pure JavaScript

var e = document.createElement('link');
e.id = 'zoomlink'
e.rel = 'stylesheet';  
e.type='text/css';
r.href='style/zoom.css'
document.getElementsByTagName("head")[0].appendChild(e);

There is many other way of writing the same but the idea is the same, remove the old reference element and add a new one.

2 Comments

@ert3: It is hard to test in a fiddle cause fiddle places the executed code inside a frame or something but I did try that code in a proper application before. Sorry it didn't work. Maybe it is to do with the HTML or when exactly you are calling the code to remove and re-add.
i thought of that to and while you'd think that would be the situation the click function seems to work just fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.