简体   繁体   English

AS3-调用外部Javascript

[英]AS3 - Calling external Javascript

I'm trying to pass the geolocation over Javascript to Flash. 我正在尝试通过Javascript将地理位置传给Flash。 The .SWF is embedded in a HTML site and set to AllowScriptAccess="always" . .SWF嵌入HTML站点中,并设置为AllowScriptAccess =“ always” For some reason the getGEO() function is not receiving the call. 由于某种原因,getGEO()函数未收到该调用。

JS: JS:

<script>
        function getGEO()
        {
         alert("getGEO");
            if (navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(function(position)
                {
                    lat = position.coords.latitude
                    long = position.coords.longitude;
                    passGEOToSWF(lat, long);
                });
            } else {

            }
        }

AS3: AS3:

if (ExternalInterface.available) 
{  
    try 
    {
        ExternalInterface.call("getGEO");
        ExternalInterface.addCallback("passGEOToSWF", onPassGEOToSWF);
    } 
    catch (error:SecurityError) 
    {
    } 
    catch (error:Error) 
    {
    } 
}

function onPassGEOToSWF(lat:*,long:*):void
{
    trace(lat,long);
}

What do I miss here? 我在这里想念什么?

You've got: 你有:

ExternalInterface.call("getGEO");
ExternalInterface.addCallback("passGEOToSWF", onPassGEOToSWF);

You may want to try: 您可能要尝试:

ExternalInterface.addCallback("passGEOToSWF", onPassGEOToSWF);
ExternalInterface.call("getGEO");

The external JS call may be completing before the AS3 registers the callback. 在AS3注册回调之前,外部JS调用可能已完成。


In order to get a reference to the flash movie, which you need in order to call the AS3 callback, you need a function like the following ( source ) 为了获得对Flash影片的引用,您需要调用Flash影片来调用AS3回调,您需要一个类似以下的函数( source

function getFlashMovieObject(movieName){
    if (window.document[movieName]){
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet")==-1){
        if (document.embeds &amp;&amp; document.embeds[movieName])
            return document.embeds[movieName];
    }
    else{
        return document.getElementById(movieName);
    }
}

Once you have that reference, you can call the AS3 callback. 一旦有了该引用,就可以调用AS3回调。 Here's an example: 这是一个例子:

function SendDataToFlashMovie(lat, long){
    var flashMovie=getFlashMovieObject("main_flash");
    flashMovie.passGeoToSWF(lat, long);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM