It seems to me that all modern browsers, including the latest versions of Chrome, Safari and FireFox (possibly from the very beginning), respect the order of the style and link elements in the head ; even when dynamically creating and adding at runtime using JavaScript.
For example, here red.css and green.css both indicate the background color of the body; the first sets the background color of the body, the second sets it green:
<head> <link rel="stylesheet" href="red.css" type="text/css"> <link rel="stylesheet" href="green.css" type="text/css"> </head>
As a result, the background color of the body is green, since green.css is placed after red.css and evaluated afterwards.
the ordering of elements seems to be preserved even when link elements are created dynamically and inserted into the head. For example, dynamically creating a link element that loads green.css will only set the background color of the body to green if it is inserted after red.css .
However, the only browser that doesn't seem to respect this is Internet Explorer (at least IE 7-9 doesn't work). It seems that with IE, the last added link or style element is evaluated on top of everything that has already been rated; it does not take into account the order of the tree when added at runtime.
Is adherence to custom order behavior or is it an Internet Explorer bug? How to fix it for Internet Explorer?
The idea I came up with is to dynamically remove all existing link and style elements and add them back to the same tree order - this is the order I want them to be evaluated.
Any understanding would be greatly appreciated.
UPDATE
The following is an example of more detailed code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="red.css" type="text/css"> </head> <body> <script type="text/javascript"> setTimeout(function() { </script> </body> </html>
Content red.css :
body { background-color: red; }
Content green.css :
body { background-color: green; }