{"id":345,"date":"2011-08-01T17:14:57","date_gmt":"2011-08-01T17:14:57","guid":{"rendered":"http:\/\/todepoint.com\/blog\/?p=345"},"modified":"2011-08-01T17:14:57","modified_gmt":"2011-08-01T17:14:57","slug":"injecting-and-calling-js-functions-from-within-flash-using-external","status":"publish","type":"post","link":"https:\/\/todepoint.com\/blog\/2011\/08\/01\/injecting-and-calling-js-functions-from-within-flash-using-external\/","title":{"rendered":"Injecting and calling JS functions from within flash using external"},"content":{"rendered":"<p><img loading=\"lazy\" src=\"https:\/\/todepoint.com\/blog\/wp-content\/uploads\/2011\/08\/js_injection.jpg\" alt=\"JS injection\" title=\"JS injection\" width=\"400\" height=\"125\"  \/><\/p>\n<p>I recently encountered a unique requirement in one of my projects, triggering the loading of a page in the background (outside of flash, using Ajax) from within flash.<br \/>\nOften you cannot rely on JS functions to be available, you could load public libraries (Jquery or equivalent) or inject your own functions to the DOM, I chose to inject my own.<\/p>\n<p><strong>This is how you do it:<\/strong><\/p>\n<p>1) import the external class.<\/p>\n<div style=\"background-color:#E0E0E0; border:1px solid #DDDDDD; padding:2px;\">\n<code>import flash.external.ExternalInterface;<\/code>\n<\/div>\n<p><\/p>\n<p>2) declare a constant variable with all the JS functions:<\/p>\n<div style=\"background-color:#E0E0E0; border:1px solid #DDDDDD; padding:2px;\">\n<code>private const script_js\t\t\t:XML =<br \/>\n&lt;script&gt;<br \/>\n&lt;![CDATA[<br \/>\nfunction() {<br \/>\n\tAJXFNC = {<br \/>\n\t\tajaxFunction:function(_url){<\/p>\n<p>\t\t\tvar ajaxRequest;<br \/>\n\t\t\ttry{<br \/>\n\t\t\t\t\/\/ Opera 8.0+, Firefox, Safari, Chrome<br \/>\n\t\t\t\tajaxRequest = new XMLHttpRequest();<br \/>\n\t\t\t\tajaxRequest.open(\"GET\", _url, true);<br \/>\n\t\t\t\tajaxRequest.send(null);<\/p>\n<p>\t\t\t} catch (e){<br \/>\n\t\t\t\t\/\/ Internet Explorer Browsers<br \/>\n\t\t\t\ttry{<br \/>\n\t\t\t\t\tajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");<br \/>\n\t\t\t\t\tajaxRequest.open(\"GET\", _url, true);<br \/>\n\t\t\t\t\tajaxRequest.send();<\/p>\n<p>\t\t\t\t} catch (e) {<br \/>\n\t\t\t\t\ttry{<br \/>\n\t\t\t\t\t\tajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");<br \/>\n\t\t\t\t\t\tajaxRequest.open(\"GET\", _url, true);<br \/>\n\t\t\t\t\t\tajaxRequest.send();<\/p>\n<p>\t\t\t\t\t} catch (e){<br \/>\n\t\t\t\t\t\t\/\/ Something went wrong<br \/>\n\t\t\t\t\t\treturn false;<br \/>\n\t\t\t\t\t}<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t}<br \/>\n}<br \/>\n]]&gt;<br \/>\n&lt;\/script&gt;;<\/code><\/div>\n<p><\/p>\n<p>3) inject the JS to DOM:<\/p>\n<div style=\"background-color:#E0E0E0; border:1px solid #DDDDDD; padding:2px;\">\n<code>try {<br \/>\n  if( ExternalInterface.available )ExternalInterface.call( script_js );<br \/>\n  } catch( error:Error ) {<br \/>\n  trace(\"ExternalInterface is not available\");<br \/>\n}<\/code>\n<\/div>\n<p><\/p>\n<p>4) call a function:<\/p>\n<div style=\"background-color:#E0E0E0; border:1px solid #DDDDDD; padding:2px;\">\n<code>ExternalInterface.call( \"AJXFNC.ajaxFunction\", \"http:\/\/www.google.com\" );<\/code>\n<\/div>\n<p><\/p>\n<p>Sweet isn&#8217;t it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently encountered a unique requirement in one of my projects, triggering the loading of a page in the background (outside of flash, using Ajax) from within flash. Often you cannot rely on JS functions to be available, you could load public libraries (Jquery or&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,23],"tags":[],"_links":{"self":[{"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/posts\/345"}],"collection":[{"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":0,"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"wp:attachment":[{"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todepoint.com\/blog\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}