SlideShare uma empresa Scribd logo
1 de 18
That’s a
                                            really small
                                            rhinoceros.




                                        +

Connecting Javascript and Flash
A Look into the ExternalInterface API
Our Cast of Characters

I’m so under-
dressed in that
   photo...




                  Javascript the Rhino   Flash the Snake
Pshaw.




Q: Why should we work together?
A: Because it lets you solve design problems
         that you couldn’t otherwise solve!

✤   “I want these embedded links in the           ✤   “I want to be able to select data from
    text I’m reading to take me to different          this Flex chart and then load an HTML
    chapters in this Flash movie.”                    data table onto the page for a deep dive
                                                      into the numbers.”
✤   “I want this text transcript to scroll
    while my Flash video is playing, and to       ✤   “I want to do usage reporting on user
    highlight the current line.”                      interactions with my .swf file. How can
                                                      I send these events to Google
✤   “I want to be able to filter this data table       Analytics?”
    using visual controls that aren’t part of
    standard HTML.”
Two Categories of Solutions
1. Outbound Control: Flash sends messages that affect other parts of
the page, outside of the swf




                                       Yeah, I’m
                                       the man!
                                       I mean snake.
Two Categories of Solutions
2. Inbound Control: Events that happen outside of the swf file control
what it displays
                                     Hey, don’t tread
                                         on me.


                            lol
Forget these jokers.
Let’s get into some code.
Two functions
     ActionScript:

     function showMessage(msg:String):void{
        msgHolder.text = msg;
     }




Javascript:
function makeAlert(msg){
   var label = document.getElementById('fromFlash');
   label.textContent = msg;
}
The steps

1. Make sure ExternalInterface is available

2. Set up your ActionScript callbacks

3. Have Flash call out to Javascript

4. Find your Flash object in the DOM

5a. Javascript calls ActionScript functions

5b. ActionScript calls Javascript functions
Step 1: Make sure it’s available

if (ExternalInterface.available)
{
    // do stuff
}
Step 2: Set up callbacks in ActionScript


ExternalInterface.addCallback("flashIsBetter", showMessage);



                     Name that will be used      Internal ActionScript
                     by Javascript to call the      function name.
                            function.




   (These two names can be the same or different.
   ExternalInterface will take care of mapping between the two.)
Step 3: Let Javascript know that you’re ready
You should always start by having the Flash file call out to the
Javascript. Otherwise, Javascript may try to call your ActionScript
functions before they are loaded and ready.

              ActionScript:
              ExternalInterface.call(“readyToGo”);


              Javascript:
              var flashReady = false;

              function readyToGo(){
                  alert('Flash is ready!');
                  flashReady = true;
              }
Step 4: Get your Flash object in Javascript
HTML:
<object id="demo"...>                  This is the Flash object on the page.
    ...                                Note that the id and name has been
    <embed name="demo".../>            set, so that you can get to the object
</object>                              easily with javascript.




Javascript:

function sendValueToFlash(){
   var flashObj;
   if (navigator.appName.indexOf("Microsoft") != -1) {
   
 flashObj = window["demo"];       The IE way.
   } else {
   
 flashObj = document["demo"];         The regular way.
   }
   ...
}
Step 5: Javascript calls ActionScript

Here we will use the callback that we set up in Step 2:
ActionScript:
ExternalInterface.addCallback("flashIsBetter", showMessage);




Javascript:
var text = document.getElementById('valueForFlash').value;
flashObj.flashIsBetter(text);


                We are using the external function name
                that was registered with addCallback.
Step 6: ActionScript calls Javascript
We’ve already seen this once, with our call to readyToGo.


ActionScript:

ExternalInterface.call(“makeAlert”, messageText.text);
Side-By-Side
Calling Javascript from ActionScript:
1. Create the Javascript function.

2. (No registration step required.)

3. ExternalInterface.call(“jsFunction”, ‘variable1’);




Calling ActionScript from Javascript:

1. Create the ActionScript function.

2. ExternalInterface.addCallback(“externalName”, “internalName”);

3. flashObject.externalName(‘variable1’, variable2);
A Few Gotchas
1. Flash objects in form tags break ExternalInterface in IE



2. Javascript operators in the name of the swf object break

ExternalInterface in IE



3. Ampersands get messed up when passed from Javascript

to ActionScript
Snake and Rhino celebrate their new-found friendship.


                              I guess
                        that wasn’t so bad.
                            High five!




                            .... I don’t
                           have arms.

Mais conteúdo relacionado

Mais procurados

Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011Nicholas Zakas
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesAbdul Rahman Sherzad
 
Paris Web - Javascript as a programming language
Paris Web - Javascript as a programming languageParis Web - Javascript as a programming language
Paris Web - Javascript as a programming languageMarco Cedaro
 
Redux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncRedux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncArtur Szott
 
Object Oriented Programming in Android Studio
Object Oriented Programming in Android StudioObject Oriented Programming in Android Studio
Object Oriented Programming in Android StudioMahmoodGhaemMaghami
 
The War is Over, and JavaScript has won: Living Under the JS Regime
The War is Over, and JavaScript has won: Living Under the JS RegimeThe War is Over, and JavaScript has won: Living Under the JS Regime
The War is Over, and JavaScript has won: Living Under the JS Regimematthoneycutt
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testingVikas Thange
 
Java script basics
Java script basicsJava script basics
Java script basicsJohn Smith
 
Lessons Learned: Migrating Tests to Selenium v2
Lessons Learned: Migrating Tests to Selenium v2Lessons Learned: Migrating Tests to Selenium v2
Lessons Learned: Migrating Tests to Selenium v2rogerjhu1
 
Desarrollo para Android con Groovy
Desarrollo para Android con GroovyDesarrollo para Android con Groovy
Desarrollo para Android con GroovySoftware Guru
 
Ruby on Rails from the other side of the tracks
Ruby on Rails from the other side of the tracksRuby on Rails from the other side of the tracks
Ruby on Rails from the other side of the tracksinfovore
 
REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptDeepu S Nath
 
Zepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_FinalZepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_FinalMax Pronko
 

Mais procurados (18)

Maintainable JavaScript 2011
Maintainable JavaScript 2011Maintainable JavaScript 2011
Maintainable JavaScript 2011
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
 
Paris Web - Javascript as a programming language
Paris Web - Javascript as a programming languageParis Web - Javascript as a programming language
Paris Web - Javascript as a programming language
 
Java scripts
Java scriptsJava scripts
Java scripts
 
Redux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with AsyncRedux Thunk - Fu - Fighting with Async
Redux Thunk - Fu - Fighting with Async
 
ReactJS
ReactJSReactJS
ReactJS
 
Object Oriented Programming in Android Studio
Object Oriented Programming in Android StudioObject Oriented Programming in Android Studio
Object Oriented Programming in Android Studio
 
The War is Over, and JavaScript has won: Living Under the JS Regime
The War is Over, and JavaScript has won: Living Under the JS RegimeThe War is Over, and JavaScript has won: Living Under the JS Regime
The War is Over, and JavaScript has won: Living Under the JS Regime
 
Javascript basics for automation testing
Javascript  basics for automation testingJavascript  basics for automation testing
Javascript basics for automation testing
 
The evolution of asynchronous javascript
The evolution of asynchronous javascriptThe evolution of asynchronous javascript
The evolution of asynchronous javascript
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Lessons Learned: Migrating Tests to Selenium v2
Lessons Learned: Migrating Tests to Selenium v2Lessons Learned: Migrating Tests to Selenium v2
Lessons Learned: Migrating Tests to Selenium v2
 
An introduction to Vue.js
An introduction to Vue.jsAn introduction to Vue.js
An introduction to Vue.js
 
Desarrollo para Android con Groovy
Desarrollo para Android con GroovyDesarrollo para Android con Groovy
Desarrollo para Android con Groovy
 
Ruby on Rails from the other side of the tracks
Ruby on Rails from the other side of the tracksRuby on Rails from the other side of the tracks
Ruby on Rails from the other side of the tracks
 
Dojo1.0_Tutorials
Dojo1.0_TutorialsDojo1.0_Tutorials
Dojo1.0_Tutorials
 
REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScript
 
Zepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_FinalZepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_Final
 

Semelhante a Connecting Flash and Javascript using ExternalInterface

JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4alexsaves
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of JavascriptTarek Yehia
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsMike Wilcox
 
Beyond the Basics, Debugging with Firebug and Web Inspector
Beyond the Basics, Debugging with Firebug and Web InspectorBeyond the Basics, Debugging with Firebug and Web Inspector
Beyond the Basics, Debugging with Firebug and Web InspectorSteven Roussey
 
JavaScript: DOM and jQuery
JavaScript: DOM and jQueryJavaScript: DOM and jQuery
JavaScript: DOM and jQuery維佋 唐
 
Inversion Of Control
Inversion Of ControlInversion Of Control
Inversion Of Controlbhochhi
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipseanshunjain
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webpjcozzi
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfStephieJohn
 
JQUERY TUTORIALS
JQUERY TUTORIALSJQUERY TUTORIALS
JQUERY TUTORIALSMoize Roxas
 
Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013Zyncro
 

Semelhante a Connecting Flash and Javascript using ExternalInterface (20)

All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4JavaScript 2.0 in Dreamweaver CS4
JavaScript 2.0 in Dreamweaver CS4
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
JavaScript
JavaScriptJavaScript
JavaScript
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
Professional JavaScript: AntiPatterns
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatterns
 
Beyond the Basics, Debugging with Firebug and Web Inspector
Beyond the Basics, Debugging with Firebug and Web InspectorBeyond the Basics, Debugging with Firebug and Web Inspector
Beyond the Basics, Debugging with Firebug and Web Inspector
 
JavaScript: DOM and jQuery
JavaScript: DOM and jQueryJavaScript: DOM and jQuery
JavaScript: DOM and jQuery
 
Inversion Of Control
Inversion Of ControlInversion Of Control
Inversion Of Control
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open web
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
 
Flash Testing with Selenium RC
Flash Testing with Selenium RCFlash Testing with Selenium RC
Flash Testing with Selenium RC
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
 
JQUERY TUTORIALS
JQUERY TUTORIALSJQUERY TUTORIALS
JQUERY TUTORIALS
 
Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013Zyncro zyncro apps & ui customization feb 2013
Zyncro zyncro apps & ui customization feb 2013
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Jquery
JqueryJquery
Jquery
 

Último

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Último (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Connecting Flash and Javascript using ExternalInterface

  • 1. That’s a really small rhinoceros. + Connecting Javascript and Flash A Look into the ExternalInterface API
  • 2. Our Cast of Characters I’m so under- dressed in that photo... Javascript the Rhino Flash the Snake
  • 3. Pshaw. Q: Why should we work together?
  • 4. A: Because it lets you solve design problems that you couldn’t otherwise solve! ✤ “I want these embedded links in the ✤ “I want to be able to select data from text I’m reading to take me to different this Flex chart and then load an HTML chapters in this Flash movie.” data table onto the page for a deep dive into the numbers.” ✤ “I want this text transcript to scroll while my Flash video is playing, and to ✤ “I want to do usage reporting on user highlight the current line.” interactions with my .swf file. How can I send these events to Google ✤ “I want to be able to filter this data table Analytics?” using visual controls that aren’t part of standard HTML.”
  • 5. Two Categories of Solutions 1. Outbound Control: Flash sends messages that affect other parts of the page, outside of the swf Yeah, I’m the man! I mean snake.
  • 6. Two Categories of Solutions 2. Inbound Control: Events that happen outside of the swf file control what it displays Hey, don’t tread on me. lol
  • 7. Forget these jokers. Let’s get into some code.
  • 8. Two functions ActionScript: function showMessage(msg:String):void{ msgHolder.text = msg; } Javascript: function makeAlert(msg){ var label = document.getElementById('fromFlash'); label.textContent = msg; }
  • 9. The steps 1. Make sure ExternalInterface is available 2. Set up your ActionScript callbacks 3. Have Flash call out to Javascript 4. Find your Flash object in the DOM 5a. Javascript calls ActionScript functions 5b. ActionScript calls Javascript functions
  • 10. Step 1: Make sure it’s available if (ExternalInterface.available) { // do stuff }
  • 11. Step 2: Set up callbacks in ActionScript ExternalInterface.addCallback("flashIsBetter", showMessage); Name that will be used Internal ActionScript by Javascript to call the function name. function. (These two names can be the same or different. ExternalInterface will take care of mapping between the two.)
  • 12. Step 3: Let Javascript know that you’re ready You should always start by having the Flash file call out to the Javascript. Otherwise, Javascript may try to call your ActionScript functions before they are loaded and ready. ActionScript: ExternalInterface.call(“readyToGo”); Javascript: var flashReady = false; function readyToGo(){ alert('Flash is ready!'); flashReady = true; }
  • 13. Step 4: Get your Flash object in Javascript HTML: <object id="demo"...> This is the Flash object on the page. ... Note that the id and name has been <embed name="demo".../> set, so that you can get to the object </object> easily with javascript. Javascript: function sendValueToFlash(){ var flashObj; if (navigator.appName.indexOf("Microsoft") != -1) { flashObj = window["demo"]; The IE way. } else { flashObj = document["demo"]; The regular way. } ... }
  • 14. Step 5: Javascript calls ActionScript Here we will use the callback that we set up in Step 2: ActionScript: ExternalInterface.addCallback("flashIsBetter", showMessage); Javascript: var text = document.getElementById('valueForFlash').value; flashObj.flashIsBetter(text); We are using the external function name that was registered with addCallback.
  • 15. Step 6: ActionScript calls Javascript We’ve already seen this once, with our call to readyToGo. ActionScript: ExternalInterface.call(“makeAlert”, messageText.text);
  • 16. Side-By-Side Calling Javascript from ActionScript: 1. Create the Javascript function. 2. (No registration step required.) 3. ExternalInterface.call(“jsFunction”, ‘variable1’); Calling ActionScript from Javascript: 1. Create the ActionScript function. 2. ExternalInterface.addCallback(“externalName”, “internalName”); 3. flashObject.externalName(‘variable1’, variable2);
  • 17. A Few Gotchas 1. Flash objects in form tags break ExternalInterface in IE 2. Javascript operators in the name of the swf object break ExternalInterface in IE 3. Ampersands get messed up when passed from Javascript to ActionScript
  • 18. Snake and Rhino celebrate their new-found friendship. I guess that wasn’t so bad. High five! .... I don’t have arms.