nytimes / Emphasis

Dynamic Deep-Linking and Highlighting
open.nytimes.com
576 stars 70 forks source link

IE doesn't like st.innerHTML #4

Closed lirenzhu closed 12 years ago

lirenzhu commented 13 years ago

IE doesn't running addCSS() starting at line 67 in emphasis-src.js, and the reason is st.innerHTML. If you try opening any article on NYTimes (and any subsequent websites that have implemented this version of Emphasis) in IE, you will see an "Unknown runtime error."

I've recently added a jQuery fork of Emphasis onto my own site and ran into the same problem. Here's the fix I employed:

addCSS: function() {
/*  Inject the minimum styles rules required */
    var st = document.createElement('style');
    // for xhtml validation goodness
    st.setAttribute('type', 'text/css');

    // placed all the css in a variable
    var stStr = 'p.' + this.classActive + ' span { background-color:#f2f4f5; } p span.' + this.classHighlight + ' { background-color:#fff0b3; } span.' + this.classInfo + ' { position:absolute; margin:-1px 0px 0px -8px; padding:0; font-size:10px; background-color: transparent !important} span.' + this.classInfo + ' a { text-decoration: none; } a.' + this.classActiveAnchor + ' { color: #000; font-size: 11px; }';

    // try it the current way, if it fails (and it will in IE) do it the way that works in IE.
    try {
      st.innerHTML = stStr;
    } catch(e) {
      st.styleSheet.cssText = stStr;
    }

    document.getElementsByTagName("head")[0].appendChild(st);
},
donohoe commented 12 years ago

If you want to do a Pull request with this change I'll test and include it

benbalter commented 12 years ago

I believe this was fixed and merged earlier downstream. See dcb5fcac28b74420dab23510ae32e0b78ad8afc3.

donohoe commented 12 years ago

Unless it pops up again I'm closing for now