有的时候,我们想要同一个html随机调用加载不同的flash。这个时候,js就派上用场了。
至于兼容性。还待测试。
说说思路。
1.建立三个数组,分别存储所想要调用的flash的url、调用flash的宽高、浏览器的title。
var flash_path = ["a1.swf","a2.swf","a3.swf"] ; var flash_attribute = [["1400","700"],["1280","700"],["1920","850"]]; var title_array = ["不明觉厉","十动然拒","人艰不拆"];
2.产生随机数,取到此次想调用的flash的url,宽高属性,以及title。
var flash_rand = Math.floor(Math.random() * flash_path.length + 1); var path = flash_path[flash_rand - 1]; var title = title_array[flash_rand - 1];
3.填充定义的html中嵌入flash的字符串。
var flash_string = '\ <object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+flash_attribute[flash_rand - 1][0]+'" height="'+flash_attribute[flash_rand - 1][1]+'">\ <param name="movie" value="'+ path +'" />\ <param name="allowScriptAccess" value="sameDomain"/>\ <param name="allowFullScreen" value="true" />\ <param name="allowFullScreenInteractive" value="true" />\ <param name="quality" value="high" />\ <param name="wmode" value="opaque" />\ <embed src= "'+ path +'" allowFullScreen="true" name="FlashID" quality="high" wmode="opaque" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+flash_attribute[flash_rand - 1][0]+'" height="'+flash_attribute[flash_rand - 1][1]+'"></embed>\ </object>';
4.将定义好的字符串写入文档。并关闭。
document.write(flash_string);
document.close();
下边完整code:
<body bgcolor="#000000" style="margin:0px;text-align:center"> <script type="text/javascript"> initFlash(); function initFlash() { var flash_path = ["a1.swf","a2.swf","a3.swf"]; var flash_attribute = [["1400","700"],["1280","700"],["1920","850"]]; var title_array = ["不明觉厉","十动然拒","人艰不拆"]; var flash_rand = Math.floor(Math.random() * flash_path.length + 1); var path = flash_path[flash_rand - 1]; var title = title_array[flash_rand - 1]; var flash_string = '\ <object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+flash_attribute[flash_rand - 1][0]+'" height="'+flash_attribute[flash_rand - 1][1]+'">\ <param name="movie" value="'+ path +'" />\ <param name="allowScriptAccess" value="sameDomain"/>\ <param name="allowFullScreen" value="true" />\ <param name="allowFullScreenInteractive" value="true" />\ <param name="quality" value="high" />\ <param name="wmode" value="opaque" />\ <embed src= "'+ path +'" allowFullScreen="true" name="FlashID" quality="high" wmode="opaque" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+flash_attribute[flash_rand - 1][0]+'" height="'+flash_attribute[flash_rand - 1][1]+'"></embed>\ </object>'; document.write(flash_string); document.close(); } </script> </body>