从 javascript/jquery 调用 AS 函数

发布于 2024-10-09 06:39:30 字数 759 浏览 6 评论 0原文

使用jquery,JS我们可以在flex代码中调用一个函数。下面是我有一个调用AS代码的按钮。如果可以的话,可以这样做吗?

 <script>
   function callas()
   {
    addBody();//call flex function        
   }
 </script>

 <input type="button" onclick="callas();" />

FLEX 代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">


<mx:Script>
<![CDATA[

import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;


public function addBody():void
{

  Alert.show("Got input from JS");

}
</mx:Script>


</mx:Application >

Using jquery,JS can we call a function in flex code.Below is that i have a button which calls a AS code.Can this be done if so how ?

 <script>
   function callas()
   {
    addBody();//call flex function        
   }
 </script>

 <input type="button" onclick="callas();" />

FLEX code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">


<mx:Script>
<![CDATA[

import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;


public function addBody():void
{

  Alert.show("Got input from JS");

}
</mx:Script>


</mx:Application >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-10-16 06:39:30

它应该是这样的:

Javascript

function getFlashMovie(movieName) {
    document.getElementById(movieName).setAttribute("name", movieName);
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function callas()
{
   // You need to know the ID of the object/embed tag; swfobject has an attribute for that. see http://code.google.com/p/swfobject/wiki/documentation#How_can_you_configure_your_Flash_content?
   var swfobjectID = 'myFlashObjectID'  
   //call flex function 
   getFlashMovie(swfobjectID).addBody();
}

Actionscript / flex

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
<![CDATA[

import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;
import flash.external.ExternalInterface;

//                "javascript function", flash function
ExternalInterface.addCallback("addBody", addBody);

public function addBody():void
{
  Alert.show("Got input from JS");
}
</mx:Script>

</mx:Application >

来源:
http://kb2.adobe.com/cps/156/tn_15683.html
http://code.google.com/p/swfobject/wiki/documentation#How_can_you_configure_your_Flash_content

It should be something like this:

Javascript

function getFlashMovie(movieName) {
    document.getElementById(movieName).setAttribute("name", movieName);
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function callas()
{
   // You need to know the ID of the object/embed tag; swfobject has an attribute for that. see http://code.google.com/p/swfobject/wiki/documentation#How_can_you_configure_your_Flash_content?
   var swfobjectID = 'myFlashObjectID'  
   //call flex function 
   getFlashMovie(swfobjectID).addBody();
}

Actionscript / flex

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
<![CDATA[

import mx.controls.Button;
import mx.controls.Alert;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.media.*;
import flash.net.*;
import flash.external.ExternalInterface;

//                "javascript function", flash function
ExternalInterface.addCallback("addBody", addBody);

public function addBody():void
{
  Alert.show("Got input from JS");
}
</mx:Script>

</mx:Application >

sources:
http://kb2.adobe.com/cps/156/tn_15683.html
http://code.google.com/p/swfobject/wiki/documentation#How_can_you_configure_your_Flash_content?

水溶 2024-10-16 06:39:30

您需要利用名为 ExternalInterface 的 Actionscript 类来与 Javascript 交互。

因此,如果您想从 Flex 或 Flash 调用 Javascript 函数,那么您应该使用类似以下内容:

ExternalInterface.call("Javscriptfunction", parameters);

如果您想从 Javascript 调用 Actionscript,请尝试以下操作:

ExternalInterface.addCallback("javascriptfunc", flexfunc);

protected function flexfunc(result:String):void{
    trace(result);
}

You need to utilize an Actionscript class named ExternalInterface to interact with Javascript.

So, if you'd like to call a Javascript function from Flex or Flash, then you should use something like this:

ExternalInterface.call("Javscriptfunction", parameters);

Should you want to call Actionscript from Javascript, try the following:

ExternalInterface.addCallback("javascriptfunc", flexfunc);

protected function flexfunc(result:String):void{
    trace(result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文