Blog

ActionScript和网页JavaScript互相调用

JavaScript和ActionScript互相调用时,其实有个非常好的特性,不过貌似很少有人提到这个特性:它们之间的数据类型对方均可以识别。因此,你可以通过swf提供的接口来传递对象、数组、字符串等等。不过它们之间传递的参数的值的长度有限制(具体长度还没仔细测试),因此你不能把JavaScript中的一个超大的对象直接传递进去。不过即使是这样,已经非常好用了,看看示例代码: as2 代码:
  1. import flash.external.*;
  2. // 创建一个行的TextField来显示调用结果
  3. var t:TextField = this.createTextField(’t’, this.getNextHighestDepth(), 10, 10, 300, 200);
  4. t.text = ’ready’;
  5. // 这个是要给JavaScript调用的函数
  6. function test(obj){
  7. var s = [];
  8. for(var p in obj){
  9. s.push(p + ’: ’ + obj
  10. );
  11. }
  12. t.text = s.join(’rn’);
  13. }
  14. // 添加调用接口
  15. var testsuc:Boolean = ExternalInterface.addCallback("test", null, test);
  16. t.text = [’test: ’, testsuc].join(’n’);
  17. // 调用JavaScript,告诉JavaScript我已经准备好了
  18. ExternalInterface.call(’ready’, {a:’a string’, b:’string b’});
JavaScript代码:
  1. var swfId = ’call_swf’
  2. function ready(){
  3. // 获取flash传递过来的参数
  4. var o = arguments[0];
  5. var s = [];
  6. for(var p in o){
  7. s.push([p, ’: ’, o
  8. ].join(’’));
  9. }
  10. // 打印传递进来的对象的内容
  11. alert(s.join(’n’));
  12. var swf = document[swfId] ? document[swfId] : window[swfId];
  13. // 调用flash提供的接口
  14. swf.test({a:’a string’, b:’string b’});
  15. }

Words translated by CCJK

146,096,379

Request a call or contact us

  •  

    Call ccjk.support for professional assistance

  •  

    Click Here to leave us a message and be as detailed as possible.

We are Certified

Our Client Satisfaction

rating for previous quarte

4.00
  •  
    Inquiry Form

    Please take few minutes to fill
    inquiry form and get Free Quote

  •  
    Request a Call

    Click to receive Free Call, Our
    Representative will contact you

  •  
    Leave a Message

    Please leave us a message and
    be as detailed as possible

Over 95% of our clients recommend our language services to others


Copyright © CCJK Technologies Co., Ltd. 2000-2017. All rights reserved.
TOP