0

I have created a bookmarklet that executes the below code, adding css styling to the page. It works on all tried sites in Chrome and Firefox, but fails for some sites on IE. It's always the same sites that fail.

The fourth line fails with "Unexpected call to method or property access" for SOME sites, only on IE.

var head = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(""));
head.appendChild(style);

Two sites that fail on IE 10:

http://www.momswhothink.com/cake-recipes/banana-cake-recipe.html

http://www.bakerella.com/
Cezar
  • 55,636
  • 19
  • 86
  • 87
user984003
  • 28,050
  • 64
  • 189
  • 285

4 Answers4

3

I think your problem is this line:

style.appendChild(document.createTextNode(""));

Here you are inserting a Text Node into a Style element, which according to the HTML specification is not allowed, unless you specify a scoped attribute on the style.

Check the specification of style here (Text Node is flow content).

You can find good ways to create the style element in a crossbrowser way here.

Community
  • 1
  • 1
Jonathan Naguin
  • 14,526
  • 6
  • 46
  • 75
  • I'm not sure how that translates into a solution? How should I set this? – user984003 Aug 23 '13 at 19:34
  • Check this post http://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript There are several good ways of create the `style` element in a crossbrowser way – Jonathan Naguin Aug 23 '13 at 19:37
  • That nailed it! I added style.type = 'text/css'; and now it works. Weird that it's only an issue with some sites. Want to include this with your answer? – user984003 Aug 23 '13 at 19:53
  • @user984003 I included the link to post, which contains your solution then :) – Jonathan Naguin Aug 23 '13 at 20:06
0

probably because you forgot to add document.ready()

Using jquery

$(document).ready(function(){
  var head = document.getElementsByTagName('head')[0];
  var style = document.createElement('style');
  style.type = 'text/css';
  style.appendChild(document.createTextNode(""));
  head.appendChild(style);
});

Using javascript

Try wrapping your javascript in an onload function. So first add:

<body onload="load()">

function load() {
      var head = document.getElementsByTagName('head')[0];
      var style = document.createElement('style');
      style.type = 'text/css';
      style.appendChild(document.createTextNode(""));
      head.appendChild(style);
}
Ahmed Alaa El-Din
  • 1,813
  • 1
  • 16
  • 19
0

I'm not sure why you're getting that error in IE10... I know in IE<9 an error is thrown if you try to modify the innerHTML of a <style> tag. It's still doable, you just have to do a bit of a workaround. For example (using jQuery):

var customCSS = "body { color: red; }";
var customStyle = $('<style type="text/css" />');
try {
    $(customStyle).html(customCSS); // Good browsers
} catch(error) { 
    $(customStyle)[0].styleSheet.cssText = customCSS; // IE < 9
}
$(customStyle).appendTo('head');

Hope this helps.

tomaroo
  • 2,524
  • 1
  • 19
  • 22
0

Do you really have to dynamically add the style section to the page? What about adding the attribute, itself, on the fly, like this:

$(document).ready( function() {
    $('[id="yourObjID"]').css('yourAttribute','itsvalue');
});

Adding the style section dynamically, rather than the attribute, seems like way overkill, to me.

vapcguy
  • 7,097
  • 1
  • 56
  • 52