Home >Web Front-end >CSS Tutorial >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!