actionscript + javascript

dan*_*ods 1 javascript flash actionscript

我想从嵌入的.swf文件中调用javascript函数.具体来说,我想在我的一个外部链接的javascript文件中调用一个函数:

function loadTrack(){



//Radio Mode feature by nosferathoo, more info in: https://sourceforge.net/tracker/index.php?func=detail&aid=1341940&group_id=128363&atid=711474

if (radio_mode && track_index==playlist_size-1) {

    playlist_url=playlist_array[track_index].location;

    for (i=0;i<playlist_mc.track_count;++i) {

        removeMovieClip(playlist_mc.tracks_mc["track_"+i+"_mc"]);

    }

    playlist_mc.track_count=0;

    playlist_size=0;

    track_index=0;

    autoload=true;

    autoplay=true;

    loadPlaylist();

    return(0);

}



start_btn_mc.start_btn._visible = false;

track_display_mc.display_txt.text = playlist_array[track_index].label;

if(track_display_mc.display_txt._width>track_display_mc.mask_mc._width){

    track_display_mc.onEnterFrame = scrollTitle;

}else{

    track_display_mc.onEnterFrame = null;

    track_display_mc.display_txt._x = 0;

}

mysound.loadSound(playlist_array[track_index].location,true);

play_mc.gotoAndStop(2)



//info button

if(playlist_array[track_index].info!=undefined){

    info_mc._visible = true;

    info_mc.info_btn.onPress = function(){

        getURL(playlist_array[track_index].info,"_blank")

    }

    info_mc.info_btn.onRollOver = function(){

        track_display_mc.display_txt.text = info_button_text;

    }

    info_mc.info_btn.onRollOut = function(){

        track_display_mc.display_txt.text = playlist_array[track_index].label;

    }

}else{

    info_mc._visible = false;

}

resizeUI();

_root.onEnterFrame=function(){

    //HACK doesnt need to set the volume at every enterframe

    mysound.setVolume(this.volume_level)

    var load_percent = (mysound.getBytesLoaded()/mysound.getBytesTotal())*100

    track_display_mc.loader_mc.load_bar_mc._xscale = load_percent;

    if(mysound.getBytesLoaded()==mysound.getBytesTotal()){

        //_root.onEnterFrame = null;

    }

}
Run Code Online (Sandbox Code Playgroud)

}

这是一个.as文件,我假设它以某种方式成为swf文件.我将如何解决此问题并"重新编译".as文件?

Bry*_*zak 7

让我们使用JS注入和ExternalInterface(两种方式都使用两种语言)为AS2和AS3编译这些答案

AS2:


// to use javascript injection in a url request
getURL("javascript:displayPost(" + postId + "," + feedId +");", "_self");

// to use the external interface
import flash.external.ExternalInterface;
ExternalInterface.call("displayPost",postId,feedId);
Run Code Online (Sandbox Code Playgroud)

AS3:


// to use javascript injection in a url request
navigateToURL(new URLRequest("javascript:displayPost(" + postId + "," + feedId +");"), "_self");

// to use the external interface
import flash.external.ExternalInterface;
ExternalInterface.call("displayPost",postId,feedId);
Run Code Online (Sandbox Code Playgroud)

请注意,在AS2和AS3中,ExternalInterface方法完全相同(在Flash 8中为AS2引入了ExternalInterface).在AS2和AS3中,javascript注入方法是相同的,除了它是navigateToURL而不是getURL,并且url字符串包装在新的URLRequest()中,因为它需要一个URLRequest对象.此外,在使用javascript注入时,最好将目标窗口设置为"_self"以避免打开新的选项卡或窗口.