Home >Web Front-end >CSS Tutorial >Why Doesn\'t Dynamic CSS Loading Work in IE, and How Can I Fix It?

Why Doesn\'t Dynamic CSS Loading Work in IE, and How Can I Fix It?

Barbara Streisand
Barbara StreisandOriginal
2024-11-29 18:14:14119browse

Why Doesn't Dynamic CSS Loading Work in IE, and How Can I Fix It?

Dynamically Loading CSS Stylesheet Not Supported in IE

In this scenario, a dynamic loading of a CSS stylesheet is attempted using jQuery. This technique is successful in Firefox and Google Chrome, however, it fails in IE.

The Solution

In IE, when all styles loaded with the page have been processed, the only reliable method to introduce an additional stylesheet is through document.createStyleSheet(url). Additional information on this approach is available in the MSDN article on createStyleSheet.

Here's the revised code incorporating the solution:

url = 'style.css';
if (document.createStyleSheet)
{
    document.createStyleSheet(url);
}
else
{
    $('<link rel="stylesheet" type="text/css" href="' + url + '" />').appendTo('head'); 
}

This approach ensures compatibility with IE while maintaining functionality in other browsers.

The above is the detailed content of Why Doesn\'t Dynamic CSS Loading Work in IE, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn