Tuesday, April 25, 2006

Dynamically Inserting FSCommand-Capable Flash Objects With innerHTML

Let me preface this post by saying that if you've never tried to dynamically write a bunch of FSCommand-enabled swfs into an HTML page, you'll probably be really confused by what follows. In point of fact, there's a good likelihood you'll be confused anyway, since my writing is not particulary lucid. But glean what you can. Forging ahead...

Suppose you have a series of SWF files to dynamically insert into a page (maybe, just maybe, as a result of the infernal Eolas patch) and these SWF files need to communicate via FSCommand. You must also add a script block for each swf, to capture the FSCommand calls. Typically, this would be a block of VB Script, like the one Flash puts into the HTML files it publishes with your SWF.

Now suppose we have the filenames of the swfs in an array, and we want to loop through the array and write each swf into the document. We need to dynamically create an ID for each one, create a VBScript for it, and then insert them into the document. Since you can't use innerHTML to insert VB Script, you have to use the DOM:

for(var i=0; i < swfArray.length; i++){
var mySwfId = "swf"+i;

var myVB = document.createElement("script");
myVB.language = "VBScript";
myVB.text = "Sub "+mySwfId+"_FSCommand(ByVal command, ByVal args) \n call "+mySwfId+"_DoFSCommand(command, args) \n end sub ";
// Insert the VBScript into the Head element
document.getElementsByTagName("head")[0].appendChild(myVB);

// Insert the swf into the target element
var mySwf = '< object id='+mySwfId+' ... swf code omitted ... < / object>';
targetElement.innerHTML = mySwf;
}

This method works pretty well as long as the VBScript is inserted before the swf is, and as long as you're doing this before the page load completes. For some reason, the VBScript doesn't seem to work if it is written into the page after loading completes. I fooled around with some techniques for simulating onDomReady in IE, but while some of them worked locally, none of them worked under live network conditions. This meant that I had to put my init() call before the closing body tag, which I hate to do because it makes my code less maintainable.

Happily, it turns out that you can use JavaScript instead of VBScript if you prefer, and JavaScript is much more forgiving in this matter. You can also insert the JavaScript via the innerHTML property:

for(var i=0; i < swfArray.length; i++){
var mySwfId = "swf"+i;

var myJS = '< script event=FSCommand(command,args) for='+mySwfId+'>\n '+mySwfId+'_DoFSCommand <\ /script>';

var mySwf = '< object id='+mySwfId+' ... swf code omitted ... < / object>';

// Insert the swf and the JavaScript into the target element.
targetElement.innerHTML = mySwf+myJS;
}

The order here is important: the JS must be inserted AFTER the swf for it to work if the swf is inserted after page load. If you insert the swf onDomReady, it seems to work in either order. I always add the JS after the swf just to be on the safe side.

Here's where I found out about using JavaScript instead of VBScript to catch the FSCommand calls.

I know this post is a bit confusing, since the code is pseudo-code and out of context. But I'll do my best to answer any questions you might have. At any rate, maybe this will help someone who, like me, was Googling for hours trying to find the answer to this obscure question.

Was this post helpful to you? If so, please consider making a small donation to keep this blog going.

11 Comments:

Blogger ä¼ ä¸–私服 said...

传世私服
传奇世界私服
传世私服
最新传世私服
跑跑卡丁车
传奇世界私服
最新传世私服
传世私服
传奇私服
千年私服
最新千年私服
千年私服
新开千年私服
新开传世私服
奇迹私服
变态奇迹私服
奇迹私服ip
千年私服
传奇私服

2:24 AM  
Blogger Ritika said...

this is not working for me
the script gets appended through innerHTML but still fscommand is not running
plz help

11:10 PM  
Blogger tom said...

If you can post your code on a webpage somewhere and post the url here, I'll take a look and see if I can spot the problem.

7:28 AM  
Blogger Manish said...

Hey Thanks! This really helped me a lot...Although I had to modify the script a bit...Here is the script-

myJS='<'+'script event=FSCommand(command,args) for='+mySwfId+'>\n'+mySwfId+'_DoFSCommand(command,args)<\/script>';

7:30 AM  
Anonymous Orme said...

Non of this work properly in ie6,7.
You don't need to write id_DoFSCommand inside 'script' tag. You just get two variables with names you defined in event listener code (it is command and args in your example). Try this:

<script event=\"FSCommand(command, args)\" for=\"rich\">\n alert('command: '+command+', args: '+args) <\/script>
Much simplier.

4:39 AM  
Anonymous Orme said...

Sorry, this will be correct:

<script event='FSCommand(command, args)' for='YOUR_SWF_ELEMENT_ID'> alert('command: '+command+', args: '+args) <\/script>

4:47 AM  
Anonymous Anonymous said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒,G點,跳蛋,跳蛋,跳蛋,性感內衣,飛機杯,充氣娃娃,情趣娃娃,角色扮演,性感睡衣,SM,潤滑液,威而柔,香水,精油,芳香精油,自慰套,自慰,性感吊帶襪,吊帶襪,情趣用品加盟AIO交友愛情館,情人歡愉用品,美女視訊,情色交友,視訊交友,辣妹視訊,美女交友,嘟嘟成人網,成人網站,A片,A片下載,免費A片,免費A片下載愛情公寓,情色,舊情人,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,一葉情貼圖片區,情色小說,色情,色情遊戲,情色視訊,情色電影,aio交友愛情館,色情a片,一夜情,辣妹視訊,視訊聊天室,免費視訊聊天,免費視訊,視訊,視訊美女,美女視訊,視訊交友,視訊聊天,免費視訊聊天室,情人視訊網,影音視訊聊天室,視訊交友90739,成人影片,成人交友,美女交友,微風成人,嘟嘟成人網,成人貼圖,成人電影,A片,豆豆聊天室,聊天室,UT聊天室,尋夢園聊天室,男同志聊天室,UT男同志聊天室,聊天室尋夢園,080聊天室,080苗栗人聊天室,6K聊天室,女同志聊天室,小高聊天室,上班族聊天室,080中部人聊天室,同志聊天室,聊天室交友,中部人聊天室,成人聊天室,一夜情聊天室,情色聊天室,寄情築園小遊戲情境坊歡愉用品,情趣用品,成人網站,情人節禮物,情人節,AIO交友愛情館,情色,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,七夕情人節,色情,情色電影,色情網站,辣妹視訊,視訊聊天室,情色視訊,免費視訊聊天,美女視訊,視訊美女,美女交友,美女,情色交友,成人交友,自拍,本土自拍,情人視訊網,視訊交友90739,生日禮物,情色論壇,正妹牆,免費A片下載,AV女優,成人影片,色情A片,成人論壇,情趣,免費成人影片,成人電影,成人影城,愛情公寓,成人影片,保險套,舊情人,微風成人,成人,成人遊戲,成人光碟,色情遊戲,跳蛋,按摩棒,一夜情,男同志聊天室,肛交,口交,性交,援交,免費視訊交友,視訊交友,一葉情貼圖片區,性愛,視訊,視訊聊天,A片,A片下載,免費A片,嘟嘟成人網,寄情築園小遊戲,女同志聊天室,免費視訊聊天室,一夜情聊天室,聊天室

10:18 AM  
Anonymous Anonymous said...

看房子,買房子,建商自售,自售,台北新成屋,台北豪宅,新成屋,豪宅,美髮儀器,美髮,儀器,髮型,EMBA,MBA,學位,EMBA,專業認證,認證課程,博士學位,DBA,PHD,在職進修,碩士學位,推廣教育,DBA,進修課程,碩士學位,網路廣告,關鍵字廣告,關鍵字,廣告,課程介紹,學分班,文憑,牛樟芝,段木,牛樟菇,日式料理, 台北居酒屋,燒肉,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,台北結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,住宿,訂房,HOTEL,飯店,造型系列,學位,牛樟芝,腦磷脂,磷脂絲胺酸,SEO,婚宴,捷運,學區,美髮,儀器,髮型,牛樟芝,腦磷脂,磷脂絲胺酸,看房子,買房子,建商自售,自售,房子,捷運,學區,台北新成屋,台北豪宅,新成屋,豪宅,學位,碩士學位,進修,在職進修, 課程,教育,學位,證照,mba,文憑,學分班,網路廣告,關鍵字廣告,關鍵字,SEO,关键词,网络广告,关键词广告,SEO,关键词,网络广告,关键词广告,SEO,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,居酒屋,燒烤,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,小套房,小套房,進修,在職進修,留學,證照,MBA,EMBA,留學,MBA,EMBA,留學,進修,在職進修,牛樟芝,段木,牛樟菇,住宿,民宿,飯宿,旅遊,住宿,民宿,飯宿,旅遊,住宿,民宿,飯宿,旅遊,住宿,民宿,飯宿,旅遊,住宿,民宿,飯宿,旅遊,住宿,民宿,飯宿,旅遊,住宿,民宿,飯宿,旅遊,美容,美髮,整形,造型,美容,美髮,整形,造型,美容,美髮,整形,造型,美容,美髮,整形,造型,美容,美髮,整形,造型,美容,美髮,整形,造型,美容,美髮,整形,造型,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,設計,室內設計,裝潢,房地產,進修,在職進修,MBA,EMBA,進修,在職進修,MBA,EMBA,進修,在職進修,MBA,EMBA,進修,在職進修,MBA,EMBA,進修,在職進修,MBA,EMBA,進修,在職進修,MBA,EMBA,進修,在職進修,MBA,EMBA,住宿,民宿,飯店,旅遊,美容,美髮,整形,造型,設計,室內設計,裝潢,房地產,進修,在職進修,MBA,EMBA,關鍵字排名,網路行銷,关键词排名,网络营销,網路行銷,關鍵字排名,关键词排名,网络营销,羅志祥,周杰倫,五月天,蔡依林,林志玲,羅志祥,周杰倫,五月天,蔡依林,林志玲,PMP,在職專班,研究所在職專班,碩士在職專班,PMP,證照,在職專班,研究所在職專班,碩士在職專班,網頁設計,網站設計,網頁設計,網站設計,网页设计,网站设计,网站设计,网页设计

1:00 AM  
Blogger Kevin said...

牙醫,植牙,假牙|矯正|牙周病,牙醫診所植牙,紋身,刺青,創業,批發,TATTOO,皮膚科,痘痘,雷射脈衝光除斑,中醫,腫瘤,腎臟病,僵直性脊椎炎,飛梭雷射,肉毒桿菌,玻尿酸,痘痘,脈衝光,醫美,毛孔粗大,醫學美容,seo,關鍵字行銷,關鍵字自然排序,網路行銷,關鍵字自然排序,關鍵字行銷seo,關鍵字廣告,網路行銷,seo,關鍵字行銷,關鍵字廣告,關鍵字,自然排序,部落格行銷,網路行銷,網路爆紅,牛舌餅婚紗台中婚紗腫瘤腎臟病

1:14 AM  
Blogger kevin said...

This comment has been removed by the author.

6:27 PM  
Blogger as said...

I like your blog. Thank you. They are really great . Ermunterung ++ .
Some new style Puma Speed is in fashion this year.
chaussure puma is Puma shoes in french . Many Franzose like seach “chaussure sport” by the internet when they need buy the Puma Shoes Or nike max shoes. The information age is really convenient .




By the way ,the nike max ltd is really good NIKE air shoes ,don’t forget buy the puma mens shoes and nike air max ltd by the internet when you need them . Do you know Nike Air Shoes is a best Air Shoes . another kinds of Nike shoes is better . For example , Nike Air Rift is good and Cheap Nike Shoes .the nike shox shoes is fitting to running.



Spring is coming, Do you think this season is not for Ugg Boots? maybe yes .but this season is best time that can buy the cheap ugg boots. Many sellers are selling discounted. Do not miss . Please view my fc2 blog and hair straighteners blog.
.thank you .


I like orange converse shoes ,I like to buy the cheap converse shoes by the internet shop . the puma shoes and the adidas shoes (or addidas shoes) are more on internet shop .i can buy the cheap nike shoes and cheap puma shoes online. It’s really convenient.
Many persons more like Puma basket shoes than nike air rift shoes . the Puma Cat shoes is a kind of Cheap Puma Shoes .
If you want to buy the Cheap Nike Air shoes ,you can buy them online. They are same as the Nike Air shoes authorized shop. Very high-caliber Air shoes and puma cat shoes . the cheap puma shoes as same as other.




polo shirts

ralph lauren polo shirts
chaussure puma

chaussure sport



chaussures puma

puma CAT

ed hardy clothing

ed hardy clothes



ed hardy womens

ed hardy sunglasses

ugg boots

cheap ugg boots

12:48 AM  

Post a Comment

Links to this post:

Create a Link

<< Home