Search code examples
javascriptfunctionactionscriptundefinedexternalinterface

Flash object function is undefined in my JavaScript


I've an asp.net website, where i have a flash object embedded. Flash object have some functions registered via ExternalInterface, so it should be accessible from JavaScript. And they are, but only when i call it when site completely loaded and i trigger some events (click etc) But when i try to access any of this function from the script block, i have an error that i call an underfined function.

This script placed at the end of document, at this moment flashGame object not underfinded, but his functions is underfined.

<script type="text/javascript">
            var flashObj = document.getElementById("flashGame");
            // AdLoaded is underfined at this moment. 
            flashObj.AdLoaded();
            // Also tried this, no luck
            $(document).ready(function () {
                flashObj.AdLoaded();
            });  
</script>

AdLoadedis underfined in both cases. P.S. And if i place a breakpoint at begin of the script it seems then all works ok, function AdLoaded() not underfined. What i miss? Thank in advance.


Solution

  • You have a ready block but the element is not in the body yet, so getElementById likely returns null,

    Try:

    $(document).ready(function () {
        var flashObj = document.getElementById("flashGame");  
        flashObj.AdLoaded();
    });  
    

    Note, that document.ready is not an indication that the flash finished loading, just that the DOM is ready, so you might have to even do:

    $(window).on("load",function () {
        var flashObj = document.getElementById("flashGame");
        flashObj.AdLoaded();
    });