2009-04-16 40 views
1

我想從嵌入式.swf文件調用JavaScript函數。具體來說,我想從內調用一個函數在我的外部鏈接的JavaScript文件之一:actionscript + 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; 

    } 

} 

}

這是在。至於文件,我認爲在某種程度上成爲swf文件。我將如何去解決這個問題並重新編譯.as文件?

回答

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

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

請注意,在AS2和AS3中ExternalInterface方法是exa ct相同(ExternalInterface在AS2的Flash 8中引入)。在AS2和AS3中,JavaScript注入方法是相同的,只不過它是navigateToURL而不是getURL,並且url字符串被封裝在新的URLRequest()中,因爲它需要URLRequest對象。另外,在使用JavaScript注入時,最好將目標窗口設置爲「_self」,以避免打開新的選項卡或窗口。

3

而且櫃面在未來的人是看這個問題Actionscript 3版本的altCo gnito的回答是這樣的:

ExternalInterface.call("displayPost",postId,feedId);