2011-06-01 29 views
6

के माध्यम से एक एक्शनस्क्रिप्ट फ़ंक्शन तक पहुंचने के लिए मैं ExternalInterface.addCallback एपीआई का उपयोग करके एक एक्शन स्क्रिप्ट में फ़ंक्शन कॉल करने का प्रयास कर रहा हूं, लेकिन मुझे इसे काम करने के लिए प्रतीत नहीं होता है। यहाँ मैं क्या है:जावास्क्रिप्ट

ActionScript:

//MyClass.as 
package { 

    import flash.display.Sprite; 
    import flash.external.ExternalInterface; 

    public class MyClass extends Sprite 
    { 
     public function MyClass() 
     { 
      ExternalInterface.addCallback('getStringJS', getStringAS); 
     } 

     public function getStringAS():String 
     { 
      return "Hello World!"; 
     } 
    } 
} 

नोट: मैं फ्लेक्स mxmlc संकलक का उपयोग कर कि अगर मायने रखती है एक swf में इस संकलन कर रहा हूँ।

HTML/Javascript नहीं:

<!doctype html> 
<html> 
    <head> 
     <title>User Identification</title> 
    <head> 
    <body> 
     <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1"> 
      <param name="movie" value="MyClass.swf"> 
      <embed src="MyClass.swf" width="1" height="1"> 
     </object> 
     <script type="text/javascript"> 
      var flash = document.getElementById("MyClass"); 
      var str = flash.getStringJS(); 
      alert(str); 
     </script> 
    </body> 
</html> 

त्रुटि मैं हो रही है:

Uncaught TypeError: Object #<HTMLObjectElement> has no method 'getStringJS' 

मैं भी एक समय समाप्ति के मामले में swf फ़ाइल लोड नहीं किया गया था में जोड़ने की कोशिश की है, लेकिन मैं उस विधि के साथ कोई सफलता नहीं थी।

किसी भी विचार?

चीयर्स,
माइक

उत्तर

5

मैंने इसे समझ लिया। ExternalInterface.call के माध्यम से javascipt को सिग्नल करने का मुख्य तरीका इसलिए हम निश्चित रूप से जानते हैं कि एसएफएफ लोड हो गया है। सबसे "यूनिवर्सल" तरीका यह है इस प्रकार है:

MyClass.as

//MyClass.as 
package { 

    import flash.display.Sprite; 
    import flash.external.ExternalInterface; 

    public class MyClass extends Sprite 
    { 
     public function MyClass() 
     { 
      ExternalInterface.addCallback('getStringJS', getStringAS); 
      if (ExternalInterface.available) { 
       ExternalInterface.call("isConnectedFlex"); 
      } 
     } 

     public function getStringAS():String 
     { 
      return "Hello World!"; 
     } 
    } 
} 

index.html

<!doctype html> 
<html> 
    <head> 
     <title>User Identification</title> 
    <head> 
    <body> 
     <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1"> 
      <param name="movie" value="MyClass.swf"> 
      <embed src="MyClass.swf" width="1" height="1"> 
     </object> 
     <script type="text/javascript"> 

      var flash = document.getElementById("MyClass"); 

      function isConnectedFlex() { 
       var str = flash.getStringJS(); 
       alert(str); 
      } 


     </script> 
    </body> 
</html> 
+0

अच्छा उदाहरण। अभी भी 2017 में काम करता है। दूसरों के लिए, कुछ अन्य अतिरिक्त जानकारी यहां भी मिल सकती है: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7cb1.html – Aleks

2

मुझे लगता है कि इस मुद्दे को फ्लैश नहीं लोड किए जाने की बात है। मैं अपने कोड window.onload घटना उपयोग करने की कोशिश और यह मेरे लिए काम किया:

फ़्लैश एक ही है ...

HTML/JS:

<!doctype html> 
<html> 
    <head> 
     <title>User Identification</title> 
    <head> 
    <body> 

     <object id="MyClass" name="MyClass" type="application/x-shockwave-flash" data="MyClass.swf" width="1" height="1"> 
      <param name="movie" value="MyClass.swf"> 
      <embed src="MyClass.swf" width="1" height="1"> 
     </object> 

     <script> 

      window.onload = function() { 

       var flash = document.getElementById("MyClass"); 
       var test = flash.getStringJS("test"); 
       alert(test); //pops up with "Hello World!" on Firefox 

      }; 

     </script> 
    </body> 
</html> 

कि मदद करता है?

+0

हाँ, यह मुझे पता है मैं वास्तव में जांच करने के लिए सीखने की जरूरत है बना मेरी अन्य ब्राउज़रों में काम हाहा। यह फ़ायरफ़ॉक्स और आईई पर काम करता है, लेकिन क्रोम नहीं। किसी भी तरह से मुझे अभी भी एक और सार्वभौमिक स्वीकार्य विधि की आवश्यकता है। कोई विचार? – Swift

+0

मैं देखता हूं। आप इस लिंक को देखना चाह सकते हैं: [समस्या-पहुंच-बाहरी-इंटरफेस-एक्सपोज़-विधि-इन-google-chrome] (http://stackoverflow.com/questions/1436722/problem-accessing-externalinterface-exposed-method-in -google-chrome) – Ian