ActionScript + JavaScript

后端 未结 3 1176
长发绾君心
长发绾君心 2021-01-16 03:01

I\'d like to call a JavaScript function from an embedded .swf file. Specifically, I\'d like to call a function in one of my externally linked JavaScript files f

3条回答
  •  时光说笑
    2021-01-16 03:30

    Let's compile those answers together for AS2 and AS3 using JS injection AND the ExternalInterface (both ways work in BOTH languages)

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

    Notice that in AS2 and AS3 the ExternalInterface method is the exact same (ExternalInterface was introduced in Flash 8 for AS2). And in AS2 and AS3 the javascript injection method are the same except that it's navigateToURL instead of getURL, and the url string is wrapped in new URLRequest(), because it needs a URLRequest object. Also when using javascript injection, it's a good practice to set the target window to "_self" to avoid a new tab or window from opening.

提交回复
热议问题