ActionScript + JavaScript

后端 未结 3 1177
长发绾君心
长发绾君心 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.

    0 讨论(0)
  • 2021-01-16 03:36
      getURL("javascript:displayPost(" + postId + "," + feedId +")");
    

    From:

    • http://www.klynch.com/archives/000079.html

    You can also look into the following:

    http://osflash.org/projects/flashjs/tutorials/jstoas

    0 讨论(0)
  • 2021-01-16 03:55

    Also incase anyone in the future is looking at this question the Actionscript 3 version of altCognito's answer is like this:

    ExternalInterface.call("displayPost",postId,feedId);
    
    0 讨论(0)
提交回复
热议问题