fgnass/spin.js — comment on commit 974cdd33719f2121bbbbfd6dbd16797fd94df98b
File:  line 0
By eych:

I also had problems with this commit in Chrome.

My theory is that sometimes when this spin.js code is loaded and working inside HEAD of the document, the part which adds the stylesheet is executed before maybe HEAD finished forming in the DOM or something like this, as a result document.styleSheets doesn't have the just added style sheet yet, at least when the problem was happening the just added spinjs stylesheet wasn't found in document.styleSheets.

So, I tried to get the stylesheet with document.getElementsByTagName('style') as this is now directly talks to DOM and this did fix the problem. But I couldn't make it work at all in IE8 (didn't try in IE7), couldn't find the needed stylesheet object. In the end, my solution works like this:

```
var sheet = (function() {
ins(document.getElementsByTagName('head')[0], createEl('style', {title: 'spinjs'}));
for (var i=0, sheets=document.styleSheets; i < sheets.length; i++) {
if (sheets[i].title == 'spinjs') return sheets[i];
}
for (var i=0, sheets=document.getElementsByTagName('style'); i < sheets.length; i++) {
if (sheets[i].title == 'spinjs') return sheets[i].sheet;
}
})();
```

At first I try to find the sheet using document.styleSheet (this works everywhere with an occasional problem in Chrome). If this fails and we reach the next for loop (probably we are in Chrome) I try to find the stylesheet using document.getElementByTagName.