1

I have some code where I am trying to include a script file and then call a function inside the script file. The code is:

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

function checkFlash(){
    includeJS('/scripts/swfobject.js');
    var playerVersion = swfobject.getFlashPlayerVersion();
    return playerVersion.major;
}

alert(checkFlash());

The problem is it is failing at checkFlash() where it tries to get the player version. I checked firebug and it shows the script is loaded and its reading the file correct, so its not a path issue.

I thought that maybe it was a delay issue in the includeJS function, but when I would that code in before the alert without it being a function, it still gives the same problem.

Any ideas on how I would accomplish something like this?

Thanks.

fanfavorite
  • 5,128
  • 1
  • 31
  • 58
  • Detecting that a ` – Pointy Nov 21 '11 at 17:14

1 Answers1

0

I think your problem is that the script file doesn't finish loading before you attempt to check the player version (just like you originally thought). You might want to try this:

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);
}

function checkFlash(){
    var playerVersion;
    includeJS('/scripts/swfobject.js', function () {
        var playerVersion = swfobject.getFlashPlayerVersion();
        alert(playerVersion.major);
    });
}

checkFlash();

This is a modified version of the solution presented in the accepted answer to this similar question.

Community
  • 1
  • 1
moff
  • 6,415
  • 31
  • 30
  • Thanks. That works great, however I am not sure how to get the playerVersion.major to be returned by the checkFlash variable or an alternate method. Would it be best to set a global variable, update it in that load function and then use that in a conditional statement? – fanfavorite Nov 21 '11 at 18:14
  • Nevermind, what I ended up doing was modifying the checkFlash to receive a function with conditions. – fanfavorite Nov 21 '11 at 19:02