0

I have a function that if called, includes a js file and uses an object from the js file. The problem is in IE9. IE9 gives me an error of 'swfobject' is undefined. All other browsers work fine. The functions are:

function checkFlash(ver,cond){
  includeJS(context+'/scripts/swfobject.js', function () { 
    var playerVersion = swfobject.getFlashPlayerVersion().major; 
    if (playerVersion < ver) {
      cond();
    }
  }); 
}

function includeJS(p_file, callback) {
  var v_js  = document.createElement('script');
  v_js.type = 'text/javascript';
  v_js.src = p_file;
  v_js.onreadystatechange = callback; 
  v_js.onload = callback; 
  document.getElementsByTagName('head')[0].appendChild(v_js);
}

Any way to get around that error? Btw, you can see this thread for more information about the functions.

I am quite confident it is not the swfobject.js file as it works fine otherwise, but in any case, here is the file.

Community
  • 1
  • 1
fanfavorite
  • 5,128
  • 1
  • 31
  • 58
  • err.. where is the swfobject variable supposed to come from? – hugomg Nov 29 '11 at 15:18
  • It's in the swfobject.js file that gets included. As I mentioned, it works fine in all other browsers except IE9. – fanfavorite Nov 29 '11 at 15:19
  • So it sounds like you're saying that this code works fine in the sense that it successfully loads the script, but the `swfobject` from the other file doesn't appear. If that's right, it sure sounds like the issue is in the other code, not this code. – RightSaidFred Nov 29 '11 at 15:21
  • No, this successfully returns a value for playerVersion in all browsers except IE9. If I include the file in the source code for IE9 it works, so there is something with IE9 in the function declaration that is calling the swfobject before it fully loads. – fanfavorite Nov 29 '11 at 15:23
  • I suspect then that this is a problem with "includeJS". It's hard to know when a browser has completely loaded a script, so it could simply be that "includeJS" is getting it wrong in the case of IE9. – Pointy Nov 29 '11 at 15:32
  • 2
    http://www.aaronpeters.nl/blog/prevent-double-callback-execution-in-IE9 – RightSaidFred Nov 29 '11 at 15:35

1 Answers1

2

I have read that onreadystatechange can fire before the data is actually loaded. Googled and got to http://msdn.microsoft.com/en-us/library/ms536957(v=vs.85).aspx and in the example is:

document.onreadystatechange=fnStartInit;
function fnStartInit()
{
   if (document.readyState=="complete")
   {
      // Finish initialization.
   }
}

But it applies to the script node too. Try some thing like:

v_js.onreadystatechange = function(){
    if (this.readyState=="complete"){
        callback();//or callback(window.event);
    }
}
Prusse
  • 4,287
  • 2
  • 24
  • 29