SlexAxton/yepnope.js — Issue #97: Failure when using with Outbrain
Author: dcmoore-gd
Created: 2012-02-21 20:45:56+00:00
State: open

This is a duplication of #46, but since that one was closed, I'm attempting to reopen.

Outbrain's main JS (http://widgets.outbrain.com/outbrain.js) dynamically appends script elements to late-load other scripts, and then when it is done, removes them. As dumb as I think it is for it to remove those, I can't really find fault with it.

Outbrain's script insertion code:

```javascript
a.m=function(b,d,f){b=c.a.createElement("script",b);b.setAttribute("src",d);b.setAttribute("type",c.b.ab);f&&b.setAttribute("async","async");return b}
...
var k=a.a.m("ob_odbCall_"+h.l,i,true);
```

Outbrain's removal code:

```javascript
a.p=function(b){if(b=OBR$(b))try{b.parentNode.removeChild(b)}catch(d){console.log(d)}};
...
a.a.p("ob_odbCall_"+h.l)
...
a.a.p("ob_odbCall_"+h)
```

So the problem, like documented in #46 is that due to occasional inconvenient timing, Outbrain's code adds a script element before yepnope's _firstScript_ is being set, but then removes it before yepnope actually uses _firstScript_.

The simplest fix would probably be to just replace occurrences of _firstScript_ with _doc.getElementsByTagName( "script" )[ 0 ]_. Even though there's still a potential race condition there, it is significantly less likely.