

|
|
Browser Quirk: Include CSS using DHTML in IEAuthored June 2007 As dynamic Web applications evolve the desire for basic "include" functionality (the ability to dynamically retrieve new content from the server and insert it seamlessly into your application) appears repeatedly. This capability is easily built using XMLHTTPRequest and the W3C DOM interfaces or (the de facto standardized, and simpler) innerHTML property of an element. One frustration met by many developers is the apparent lack of Internet Explorer support for embedded and/or linked style-sheets in included content. Including StylesWeb applications often desire the ability to load new content dynamically and unobtrusively. One example is contextual "Help" information. Help content might be pulled, using XMLHTTPRequest, from the server based on some user action and inserted into a DIV using the innerHTML property. This experience (as compared to distracting pop-up help or on-demand requests) can be exceptionally smooth. For our purposes let's assume that the dynamic content requires certain special CSS declarations. The first inclination of many developers is to include those styles at the top of the content (as they do on any "page") using a style-sheet declaration like so:
<style>
p { color: blue; }
</style>
<p>Hello, how are you!</p>
They may even try to include a seperately defined style sheet using the HTML LINK tag like this: <link rel="stylesheet" href="HelpContent.css" type="text/css"> <p>Hello, how are you!</p> (Note, for the record, that neither of these solutions results in valid HTML - both the LINK and STYLE tags are specified to appear only in the HEAD of a document. However, like many other technically invalid, but oh-so-very-useful techniques browser-support is widespread.) FireFox (2.x) and Opera (9.x) both work exactly as hoped: in either case the styles are happily and correctly applied to the content. However IE (still commanding an overwhelming share of users) dissappoints: it completely ignores the style sheets. After poking, prodding and (most likely) lots of swearing many developers give up and declare that IE is simply incapable. They retreat to more cumbersome but reliable solutions (keeping all their styles in a single, global sheet or including all their styles on every page whether it needs them or not). The solution to the problem is actually quite simple (if a bit silly and obtuse). Fixing the ProblemMove the style-sheet to the bottom of your content. That's it: you've fixed the problem. IE, for some reason, needs to display some content (anything that affects the page layout will do) before it will recognize style declarations in included content. So while the above examples will fail in IE the following example will work (in FireFox and Opera as well):
<p>Hello, how are you!</p>
<style>
p { color: blue; }
</style>
It will also work using a linked style sheet: <p>Hello, how are you!</p> <link rel="stylesheet" href="HelpContent.css" type="text/css"> Note that style declarations really don't have to appear last. As long as something (anything) above the STYLE or LINK tags affects the layout the styles will be applied. For example a single break tag would trigger the styles, but an empty paragraph would not. Things to RememberThere are many small "gotchas" when using this technique. Some of the more common are:
ConclusionWhile you may write-off this issue as just another work-around for Internet Explorer it's difficult to fault it for what amounts to support for invalid HTML. There is no standard to follow when deciding not to follow the standard. In any case it's a simple issue to work-around and the capabilities gained are cross-platform and potentially very useful. |