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
Release: 2024-11-29 18:14:14
Original
118 people have browsed it

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'); 
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template