views:

321

answers:

2

I've had a problem with my styles not being applied after AJAX calls. My styles were not in the < HEAD> section of the page, and they were only recognized by IE on initial Page_Load.

If you know of any other ways to fix this problem, post them here.

This is more of a reference, hope this helps some people.

+1  A: 

After doing some Googling, I found out that moving my styles into the < HEAD> tag of the page fixes the problem.

roman m
@rm: accepting your own answer might help others.
Jon Cram
A: 

You can also grab the style from your AJAX HTML, and insert it in the head. Here's some sample code. Tested in IE8 and Chrome.

function enable_embedded_styles(html) {
// Grab style content, and create new style element for it
// Works for first set of <style></style> tags in html
// Tested in IE and Chrome
    if (typeof(html) === 'string') {
        var beg = html.indexOf('<style>'), 
            end = html.indexOf('</style>');

        if (beg !== -1 && end !== -1) {
            var style = html.substr(beg + 7, end - 7 - beg); // everything between style tags
            html = html.substr(end + 8); // everything after closing style tag

            s = document.createElement('style');
            s.setAttribute('type','text/css');

            // For IE
            if (s.styleSheet) {
                s.styleSheet.cssText = style;
            } // endif

            // For every other browser
            else {
                s.appendChild(document.createTextNode(style));
            } // endelse

            // Append stylesheet to head
            document.getElementsByTagName('head')[0].appendChild(s);
        } // endif
    } // endif

    return html;
} // endfunction
Fraser Harris