{"id":117,"date":"2009-03-16T18:54:59","date_gmt":"2009-03-16T18:54:59","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2009\/03\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/"},"modified":"2009-03-16T18:54:59","modified_gmt":"2009-03-16T18:54:59","slug":"knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/","title":{"rendered":"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2"},"content":{"rendered":"<p>\nIn the previous article <a href=\"\/robblog\/archive\/2009\/3\/Knowing_When_It_s_Safe_To_Talk_To_Flash_From_JavaScript.shtml\">Knowing When It&#8217;s Safe To Talk To Flash From JavaScript<\/a> we saw how to call Flash from JavaScript.\n<\/p>\n<p>\nWe embedded the Flash movie directly into the page, however there are times when we may want to insert the movie dynamically using JavaScript. How can we do this?\n<\/p>\n<p>\nWell it turns out fairly easily, especially if we use one of the modern JavaScript framework libraries like <a href=\"http:\/\/jquery.com\/\">jQuery<\/a>.\n<\/p>\n<p>\njQuery is very extendable, and has a powerful plugin based architecture. Thankfully someone has done the hard work for us and written a plugin to handle the embedding of Flash. Take a look at the <a href=\"http:\/\/jquery.lukelutman.com\/plugins\/flash\/\">jQuery.flash plugin<\/a>.\n<\/p>\n<p>\nHow do we use it, well first we need to add the two libraries to the page, jQuery and jQuery.flash.\n<\/p>\n<div class=\"code\"><code>&lt;script type=\"text\/javascript\" src=\"jquery-1.3.2.js\"&gt;&lt;\/script&gt;<br \/>\n&lt;script type=\"text\/javascript\" src=\"jquery.flash.js\"&gt;&lt;\/script&gt;<br \/>\n<\/code><\/div>\n<p>\nIn our page body, we&#8217;ll add a bit of HTML that will have it&#8217;s content replaced by the Flash movie.\n<\/p>\n<div class=\"code\"><code>&lt;-- the content of the following div will be replaced. --&gt;<br \/>\n&lt;div id=\"example\"&gt;Flash here&lt;\/div&gt;<br \/>\n<\/code><\/div>\n<p>\nNow let&#8217;s add the JavaScript that will replace the div&#8217;s content. We&#8217;ll wrap it inside jQuery&#8217;s <code>ready<\/code> function to make sure everything we need is in place. This is a better version of the <code>window.onload<\/code> function we saw in the previous article that waits until the page can be manipulated, and not until everythting has been downloaded.\n<\/p>\n<div class=\"code\"><code><br \/>\n$(document).ready(function() {<br \/>\n$('#example').flash(<br \/>\n{<br \/>\nsrc: 'test.swf',<br \/>\nheight: 1,<br \/>\nwidth: 1,<br \/>\nflashvars: {onLoad: 'flashLoaded'}<br \/>\n},<br \/>\n{ version: 8}<br \/>\n);<br \/>\n});<br \/>\n<\/code><\/div>\n<p>\nThis creates the <code>object<\/code> or <code>embed<\/code> as necessary, and loads the movie &#8220;test.swf&#8221;, with a height and width of 1 pixel and the flashvars parameters we saw in the previous article. In this case, the <var>onLoad<\/var> parameter has a value of <code>flashLoaded<\/code>, the name of the function we want to call when the flash has loaded and is ready to execute. There is also a parameter saying the minimum version of the Flash player we need, let&#8217;s use version 8 here.\n<\/p>\n<p>\nNow when the page is loaded, the <code>&lt;div&gt;<\/code> block will be replaced with the flash movie, and this in turn will call our callback function <code>flashLoaded<\/code>.\n<\/p>\n<p>\n<strong>Update: Using Mootools<\/strong>\n<\/p>\n<p>\n<a href=\"http:\/\/mootools.net\/\">Mootools<\/a> is another excellent JavaScript framework library, and it has Flash support built into it&#8217;s core using the <a href=\"http:\/\/mootools.net\/docs\/Utilities\/Swiff\">Swiff<\/a> class.\n<\/p>\n<p>\nFor Mootools, firstly we need to make sure it&#8217;s loaded.\n<\/p>\n<div class=\"code\"><code>&lt;!-- load the mootools library --&gt;<br \/>\n&lt;script type=\"text\/javascript\" src=\"mootools.js\"&gt;&lt;\/script&gt;<br \/>\n<\/code><\/div>\n<p>\nNow we&#8217;ll wrap our Swiff object creation in Mootools domready event. This is the equivilant to what we did earlier in jQuery with the ready event.\n<\/p>\n<div class=\"code\"><code>  window.addEvent('domready', function() {<br \/>\nvar flash = new Swiff('test.swf', {<br \/>\ncontainer: $('example'),<br \/>\nwidth: 1,<br \/>\nheight: 1,<br \/>\ncallBacks: {<br \/>\nonLoad: flashLoaded<br \/>\n}<br \/>\n});<br \/>\n});<br \/>\n<\/code><\/div>\n<p>\nHere we&#8217;re creating a new Swiff object, and telling it to place the flash object \/ embed tag it creates inside the div with the id of <var>example<\/var> like we did with the jQuery example. We specify the height and width both of 1 pixel and setup the callback.\n<\/p>\n<p>\nAs you can see, Mootools specifically calls this a callback unlike jQuery&#8217;s flashvars. They mean the same thing here, so we say the <code>onLoad<\/code> callback function in the Flash movie&#8217;s ActionScript will call the local JavaScript function <code>flashLoaded<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous article Knowing When It&#8217;s Safe To Talk To Flash From JavaScript we saw how to call Flash from JavaScript. We embedded the Flash movie directly into the page, however there are times when we may want to insert the movie dynamically using JavaScript. How can we do this? Well it turns out &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2&#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-117","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 v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Knowing When It&#039;s Safe To Talk To Flash From JavaScript - Part 2 - 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=\"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-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 - Part 2 - Robert Price\" \/>\n<meta property=\"og:description\" content=\"In the previous article Knowing When It&#8217;s Safe To Talk To Flash From JavaScript we saw how to call Flash from JavaScript. We embedded the Flash movie directly into the page, however there are times when we may want to insert the movie dynamically using JavaScript. How can we do this? Well it turns out &hellip; Continue reading &quot;Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2009-03-16T18:54:59+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\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2\",\"datePublished\":\"2009-03-16T18:54:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/\"},\"wordCount\":474,\"keywords\":[\"JavaScript\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/\",\"name\":\"Knowing When It's Safe To Talk To Flash From JavaScript - Part 2 - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2009-03-16T18:54:59+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/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\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"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 - Part 2 - 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":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Knowing When It's Safe To Talk To Flash From JavaScript - Part 2 - Robert Price","og_description":"In the previous article Knowing When It&#8217;s Safe To Talk To Flash From JavaScript we saw how to call Flash from JavaScript. We embedded the Flash movie directly into the page, however there are times when we may want to insert the movie dynamically using JavaScript. How can we do this? Well it turns out &hellip; Continue reading \"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/","og_site_name":"Robert Price","article_published_time":"2009-03-16T18:54:59+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":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2","datePublished":"2009-03-16T18:54:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/"},"wordCount":474,"keywords":["JavaScript","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/","name":"Knowing When It's Safe To Talk To Flash From JavaScript - Part 2 - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2009-03-16T18:54:59+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/knowing_when_it_s_safe_to_talk_to_flash_from_javascript_part_2-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Knowing When It&#8217;s Safe To Talk To Flash From JavaScript &#8211; Part 2"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/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":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","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":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}