{"id":118,"date":"2009-03-12T18:35:55","date_gmt":"2009-03-12T18:35:55","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2009\/03\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/"},"modified":"2009-03-12T18:35:55","modified_gmt":"2009-03-12T18:35:55","slug":"knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml","status":"publish","type":"post","link":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/","title":{"rendered":"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript"},"content":{"rendered":"<p>\nI&#8217;ve been doing some work lately where JavaScript in an XHTML page needs to talk to some ActionScript 3.0 in a Flash movie.\n<\/p>\n<p>\nThe most common problem here is knowing when you can start talking to the Flash movie. The browser may have loaded all the scripts and started to execute them, but the flash movie may not have loaded or initialised at that point. If you try to talk to the movie at this point, you&#8217;ll get errors.\n<\/p>\n<p>\nThere are a couple of ways around this. You could code the flash movie into the page using <code>&lt;object&gt;<\/code> and <code>&lt;embed&gt;<\/code> html tags, then make sure your JavaScript calls the movie after the onload event has been called. E.g.\n<\/p>\n<div class=\"code\"><code>function flashLoaded() {<br \/>\nalert(\"Flash loaded\");<br \/>\n}<br \/>\nwindow.onload = function() {<br \/>\n\/\/ flash should have loaded by now.<br \/>\nflashLoaded();<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nNow that isn&#8217;t ideal, the flash may have loaded, but the movie may not have initialised so you could still have problems.\n<\/p>\n<p>\nThe safest way is to include a callback function in your ActionScript that runs once the movie has finished initialising. This will call some JavaScript on your page letting you know the movie has loaded and can be interacted with. We can use the <code><a href=\"http:\/\/livedocs.adobe.com\/flash\/9.0\/ActionScriptLangRefV3\/flash\/external\/ExternalInterface.html\">ExternalInterface<\/a><\/code> class in the ActionsScript to achieve this.\n<\/p>\n<p>\nLet&#8217;s assume our ActionScript class is called test, and we have a function in our JavaScript called flashLoaded. The following code is a quick way to let the JavaScript know the movie has loaded.\n<\/p>\n<div class=\"code\"><code>public function test():void<br \/>\n{<br \/>\nExternalInterface.call(\"flashLoaded\");<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nThis is great, but it does mean we&#8217;ve had to hardcode the name of the calling function. What if we had two Flash movies in the page and they both called this function. This could cause problems. We need a way to pass in a function name dynamically.\n<\/p>\n<p>\nFlash has something that used to be called <code>FlashVars<\/code> that can help us here. It&#8217;s basically a list of parameters and values that are passed and appended as a query string to the end of the movie files name. These are then available to ActionScript. In our <code>object<\/code> we alter the movie paramter accordingly.\n<\/p>\n<div class=\"code\"><code>&lt;-- in the flash object tag --&gt;<br \/>\n&lt;param name=\"movie\" value=\"test.swf?onLoad=flashLoaded\" \/&gt;<br \/>\n<\/code><\/div>\n<p>\nIf you use the <code>embed<\/code> tag, make sure you alter the movie parameter there as well.\n<\/p>\n<p>\nTo see the passed parameters in ActionScript was need to use <code>root.loaderInfo.parameters<\/code>. Our initialiser function should be altered to use the following code, in this case we want to use the <var>onLoad<\/var> parameter.\n<\/p>\n<div class=\"code\"><code>public function test():void<br \/>\n{<br \/>\nExternalInterface.call(root.loaderInfo.parameters.onLoad);<br \/>\n}<br \/>\n<\/code><\/div>\n<p>Great, we can now vary our the name of our callback. However problems could still occur. The movie may be loaded but the stage may not be ready. We need to add a little bit more wrapping code to handle this. The following code should do the trick.<\/p>\n<div class=\"code\"><code><br \/>\npackage {<br \/>\nimport flash.display.Sprite;<br \/>\nimport flash.events.*;<br \/>\nimport flash.external.ExternalInterface;<br \/>\npublic class test extends Sprite<br \/>\n{<br \/>\npublic function test():void<br \/>\n{<br \/>\nif (stage) init();<br \/>\nelse addEventListener(Event.ADDED_TO_STAGE, init);<br \/>\n}<br \/>\npublic function init(e:Event = null):void<br \/>\n{<br \/>\nremoveEventListener(Event.ADDED_TO_STAGE, init);<br \/>\nExternalInterface.call(root.loaderInfo.parameters.onLoad);<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nAs you can see we&#8217;ve moved the callback into the <code>init<\/code> function, and this is only called when the stage is ready.\n<\/p>\n<p>\nYou should now be safely able to script your flash movie from JavaScript.\n<\/p>\n<p>\nSee <a href=\"http:\/\/www.robertprice.co.uk\/robblog\/archive\/2009\/3\/Knowing_When_It_s_Safe_To_Talk_To_Flash_From_JavaScript_Part_2.shtml\">Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2<\/a> for examples on how to integrate this with a modern JavaScript library like jQuery.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing some work lately where JavaScript in an XHTML page needs to talk to some ActionScript 3.0 in a Flash movie. The most common problem here is knowing when you can start talking to the Flash movie. The browser may have loaded all the scripts and started to execute them, but the flash &hellip; <a href=\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Knowing When It&#8217;s Safe To Talk To Flash From JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,75],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Knowing When It&#039;s Safe To Talk To Flash From JavaScript - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Knowing When It&#039;s Safe To Talk To Flash From JavaScript - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been doing some work lately where JavaScript in an XHTML page needs to talk to some ActionScript 3.0 in a Flash movie. The most common problem here is knowing when you can start talking to the Flash movie. The browser may have loaded all the scripts and started to execute them, but the flash &hellip; Continue reading &quot;Knowing When It&#8217;s Safe To Talk To Flash From JavaScript&quot;\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2009-03-12T18:35:55+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript\",\"datePublished\":\"2009-03-12T18:35:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\"},\"wordCount\":498,\"keywords\":[\"JavaScript\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\",\"url\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\",\"name\":\"Knowing When It's Safe To Talk To Flash From JavaScript - Robert Price\",\"isPartOf\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\"},\"datePublished\":\"2009-03-12T18:35:55+00:00\",\"author\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.robertprice.co.uk\/robblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\",\"url\":\"http:\/\/www.robertprice.co.uk\/robblog\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Knowing When It's Safe To Talk To Flash From JavaScript - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Knowing When It's Safe To Talk To Flash From JavaScript - Robert Price","og_description":"I&#8217;ve been doing some work lately where JavaScript in an XHTML page needs to talk to some ActionScript 3.0 in a Flash movie. The most common problem here is knowing when you can start talking to the Flash movie. The browser may have loaded all the scripts and started to execute them, but the flash &hellip; Continue reading \"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript\"","og_url":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/","og_site_name":"Robert Price","article_published_time":"2009-03-12T18:35:55+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/#article","isPartOf":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/"},"author":{"name":"rob","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript","datePublished":"2009-03-12T18:35:55+00:00","mainEntityOfPage":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/"},"wordCount":498,"keywords":["JavaScript","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/","url":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/","name":"Knowing When It's Safe To Talk To Flash From JavaScript - Robert Price","isPartOf":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2009-03-12T18:35:55+00:00","author":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript"}]},{"@type":"WebSite","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website","url":"http:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":0,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}