Removing <link> element with jQuery?

Solution 1:

This is not a bug in jQuery, it is a bug (or possibly, a feature) of the IE rendering engine.

It seems this problem is being caused by the fact that Internet Explorer does not correctly re-render the page after removing the LINK element from the DOM.

In this particular case, the LINK tag is no longer present at the DOM, but IE still displays the CSS that has been loaded into memory.

A workaround / solution for this is to disable the stylesheet using the .disabled property like this:

// following code will disable the first stylesheet
// the actual DOM-reference to the element will not be removed; 
// this is particularly useful since this allows you to enable it
// again at a later stage if you'd want to.
document.styleSheets[0].disabled = true;

EDIT in reply to your comment:

Or, if you want to remove it by the href use the following code:

var styleSheets = document.styleSheets;
var href = 'http://yoursite.com/foo/bar/baz.css';
for (var i = 0; i < styleSheets.length; i++) {
    if (styleSheets[i].href == href) {
        styleSheets[i].disabled = true;
        break;
    }
}

Solution 2:

Perhaps it's something strange IE6 does to URL in the href attribute? Try something like:

$("LINK[href*='style.css']").remove();

(i.e. check whether the href value contains "style.css")

It's just a guess, however. If that doesn't work, I recommend checking the JQuery documentation closely on the subject of attribute selectors and the remove method.

Also keep in mind that it's also not impossible that it's in fact a bug. (IE6 in general causes lots of issues involving JavaScript and DOM manipulation, among other things.)

Solution 3:

Topic's quite old, but You can only add ID to your link element, and delete it by element:

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