7.4.3 Javascript端获取swf对象的引用

在Javascript代码中,我们首先要定义一个object标签对象,object标签对象的大概格式 如下:

<object type='application/x-shockwave-flash'
 data='FlexAndJs.swf' width='600' height='480'
 name='test' id='MyFlexApps'>
 <param name='allowScriptAccess' value='always' />
 <param name='movie' value='FlexAndJs.swf' />
 <param name='quality' value='high' />
 <param name='scale' value='noScale' />
 <param name='wmode' value='transparent' />
<embed src="FlexObject.swf" width="640" height="378"
   name ="FlexObject"
   play="true"
   loop="false"
   allowScriptAccess="sameDomain" 
   type="application/x-shockwave-flash"
   pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>

定义好这个节点标签后,我们还需要在写一个JS函数来获取该swf对象的引用。

function getSWFObject(movieName)
{
   if(document[movieName])
   {
         return document[movieName];
   }else if(window[movieName]){
        return window[movieName]; 
   }else if(document.embeds && document.embeds[movieName]){
        return document.embeds[movieName];
   }else{
        return document.getElementById(movieName);
   }
}

然后上文中的testFlexFunc()函数的那条调用Flex函数的语句可以改写为:

var strMessage = getSWFObject["MyFlexApps"].sayHelloFromFlex("Hello, Flex");

results matching ""

    No results matching ""