FlashObject: flash检测嵌入脚本解决相应的IE更新带来的不便

2006-05-22 11:21:06

来自:fscommand 针对之前的 Microsoft新的IE更新将影响ActiveX控件工作文中提到的flash嵌入脚本来解决相应的IE更新带来的不便,对FlashObject进行了一个本地化,如下: 首先要使用这个嵌入脚本类先要加入flashobj.js HTML 代码: [code]/** * FlashObject v1.3c: Flash detection and embed - http://blog.deconcept.com/flashobject/ * * FlashObject is (c) 2006 Geoff Stearns and is released under the MIT License: * http://www.opensource.org/licenses/mit-license.php * */ if(typeof com=="undefined"){var com=new Object();} if(typeof com.deconcept=="undefined"){com.deconcept=new Object();} if(typeof com.deconcept.util=="undefined"){com.deconcept.util=new Object();} if(typeof com.deconcept.FlashObjectUtil=="undefined"){com.deconcept.FlashObjectUtil=new Object();} com.deconcept.FlashObject=function(_1,id,w,h,_5,c,_7,_8,_9,_a,_b){ if(!document.cr&#101;ateElement||!document.getElementById){return;} this.DETECT_KEY=_b?_b:"detectflash"; this.skipDetect=com.deconcept.util.getRequestParameter(this.DETECT_KEY); this.params=new Object(); this.variables=new Object(); this.attributes=new Array(); this.useExpressInstall=_7; if(_1){this.setAttribute("swf",_1);} if(id){this.setAttribute("id",id);} if(w){this.setAttribute("width",w);} if(h){this.setAttribute("height",h);} if(_5){this.setAttribute("version",new com.deconcept.PlayerVersion(_5.toString().split(".")));} this.installedVer=com.deconcept.FlashObjectUtil.getPlayerVersion(this.getAttribute("version"),_7); if(c){this.addParam("bgcolor",c);} var q=_8?_8:"high"; this.addParam("quality",q); var _d=(_9)?_9:window.location; this.setAttribute("xiRedirectUrl",_d); this.setAttribute("redirectUrl",""); if(_a){this.setAttribute("redirectUrl",_a);} }; com.deconcept.FlashObject.prototype={setAttribute:function(_e,_f){ this.attributes[_e]=_f; },getAttribute:function(_10){ return this.attributes[_10]; },addParam:function(_11,_12){ this.params[_11]=_12; },getParams:function(){ return this.params; },addVariable:function(_13,_14){ this.variables[_13]=_14; },getVariable:function(_15){ return this.variables[_15]; },getVariables:function(){ return this.variables; },cr&#101;ateParamTag:function(n,v){ var p=document.cr&#101;ateElement("param"); p.setAttribute("name",n); p.setAttribute("value",v); return p; },getVariablePairs:function(){ var _19=new Array(); var key; var _1b=this.getVariables(); for(key in _1b){_19.push(key+"="+_1b[key]);} return _19; },getFlashHTML:function(){ var _1c=""; if(navigator.plugins&&navigator.mimeTypes&&navigator.mimeTypes.length){ if(this.getAttribute("doExpressInstall")){ this.addVariable("MMplayerType","PlugIn"); } _1c="<embed type=\"application/x-shockwave-flash\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\""; _1c+=" id=\""+this.getAttribute("id")+"\" name=\""+this.getAttribute("id")+"\" "; var _1d=this.getParams(); for(var key in _1d){_1c+=[key]+"=\""+_1d[key]+"\" ";} var _1f=this.getVariablePairs().jo&#105;n("&"); if(_1f.length>0){_1c+="flashvars=\""+_1f+"\"";} _1c+="/>"; }else{ if(this.getAttribute("doExpressInstall")){this.addVariable("MMplayerType","ActiveX");} _1c="<object id=\""+this.getAttribute("id")+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\">"; _1c+="<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />"; var _20=this.getParams(); for(var key in _20){_1c+="<param name=\""+key+"\" value=\""+_20[key]+"\" />";} var _22=this.getVariablePairs().jo&#105;n("&"); if(_22.length>0){_1c+="<param name=\"flashvars\" value=\""+_22+"\" />"; }_1c+="</object>";} return _1c; },write:function(_23){ if(this.useExpressInstall){ var _24=new com.deconcept.PlayerVersion([6,0,65]); if(this.installedVer.versionIsValid(_24)&&!this.installedVer.versionIsValid(this.getAttribute("version"))){ this.setAttribute("doExpressInstall",true); this.addVariable("MMredirectURL",escape(this.getAttribute("xiRedirectUrl"))); document.title=document.title.slice(0,47)+" - Flash Player Installation"; this.addVariable("MMdoctitle",document.title);} }else{this.setAttribute("doExpressInstall",false);} if(this.skipDetect||this.getAttribute("doExpressInstall")||this.installedVer.versionIsValid(this.getAttribute("version"))){ var n=(typeof _23=="string")?document.getElementById(_23):_23; n.innerHTML=this.getFlashHTML(); }else{if(this.getAttribute("redirectUrl")!=""){document.location.replace(this.getAttribute("redirectUrl"));}}}}; com.deconcept.FlashObjectUtil.getPlayerVersion=function(_26,_27){ var _28=new com.deconcept.PlayerVersion(0,0,0); if(navigator.plugins&&navigator.mimeTypes.length){ var x=navigator.plugins["Shockwave Flash"]; if(x&&x.description){_28=new com.deconcept.PlayerVersion(x.description.replace(/([a-z]|[A-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));} }else{ try{var axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash"); for(var i=3;axo!=null;i++){ axo=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i); _28=new com.deconcept.PlayerVersion([i,0,0]);}} catch(e){} if(_26&&_28.major>_26.major){return _28;} if(!_26||((_26.minor!=0||_26.rev!=0)&&_28.major==_26.major)||_28.major!=6||_27){ try{ _28=new com.deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(",")); }catch(e){}}} return _28; }; com.deconcept.PlayerVersion=function(_2c){ this.major=parseInt(_2c[0])||0; this.minor=parseInt(_2c[1])||0; this.rev=parseInt(_2c[2])||0; }; com.deconcept.PlayerVersion.prototype.versionIsValid=function(fv){ if(this.major<fv.major){return false;} if(this.major>fv.major){return true;} if(this.minor<fv.minor){return false;} if(this.minor>fv.minor){return true;} if(this.rev<fv.rev){return false;} return true; }; com.deconcept.util={getRequestParameter:function(_2e){ var q=document.location.search||document.location.href.hash; if(q){var _30=q.indexOf(_2e+"="); var _31=(q.indexOf("&",_30)>-1)?q.indexOf("&",_30):q.length; if(q.length>1&&_30>-1){ return q.substring(q.indexOf("=",_30)+1,_31);}}return ""; },removeChildren:function(n){ while(n.hasChildNodes()){ n.removeChild(n.firstChild);}}}; if(Array.prototype.push==null){ Array.prototype.push=function(_33){ this[this.length]=_33; return this.length;};} var getQueryParamValue=com.deconcept.util.getRequestParameter; var FlashObject=com.deconcept.FlashObject; [/code] 页面代码如下: HTML 代码: [code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=“http://www.w3.org/1999/xhtml”> <head> <title>Flash对象插入脚本</title> <meta http-equiv=“Content-Type” content=“text/html; charset=gb2312″ /> <style type=“text/css”> <!– html, body, ul, ol, li, p, h1, h2, h3, h4, h5, h6, form, fieldset, table, td, img { margin: 0; padding: 0; border: 0; font-size:12px; } body{background-color: #fff;margin:5px;} td,p,li,sel&#101;ct,input,textarea{font-size:12px;} .f14{font-size:14px;} .b1{border:1px solid #000000;} #flashVar {color:#7793FB;border:1px solid #7793FB;width:320px;margin:10px 0;} –> </style> <script language=“JavaScript” type=“text/javascript” src=“flashobj.js”></script> <noscript> 您的浏览器不支持javascript </noscript> </head> <body> <div id=“flashcontent”> <strong>升级一下吧,费不了几分钟</strong> </div> <div id=“flashVar”></div> <script type=“text/javascript”> // <![CDATA[ var fo = new FlashObject(“fo_tester.swf”, “fotester”, “320″, “240″, “8.0.22″, “#FF6600″, false, “High”, “http://www.sina.com.cn/”, “http://www.sohu.com/”,false); //(swf地址, 嵌入的flash对象id, 宽度w, 高度h, 版本ver, 背景颜色c, 是否强制使用自动快速升级安装useExpressInstall, 质量quality, 升级后跳转地址xiRedirectUrl, 不升级跳转地址redirectUrl, 是否检测播放器版本detectKey) //( fo.addVariable(“flashVarText”, “传值123″); // 传递参数进flash fo.addVariable(“flashVarText1″, “传值123″); //设置参数 fo.addParam(“MENU”,true); fo.addParam(“SWLIVECONNECT”,true); fo.addParam(“QUALITY”,“Best”); fo.addParam(“WMODE”,“window”); fo.write(“flashcontent”); //document.getElementById(“flashVar”).innerHTML = fo.getVariable(“flashVarText”); VariablePairs = fo.getVariablePairs(“flashVarText”); document.getElementById(“flashVar”).innerHTML = VariablePairs[0]; // ]]> </script> </body> </html>[/code] 在做播放器版本快速升级的时候,要在flash文件中进行操作,actionscript如下: HTML 代码 [code]/** * expressinstall.as v1.0 - http://blog.deconcept.com/flashobject/ * * 08-12-2005 (c) 2006 Geoff Stearns and is released under the MIT License: * http://www.opensource.org/licenses/mit-license.php * * Updated: 12-20-2005 * *使用此文件调用Macromedia Flash Player快速安装功能,此文件与Flash嵌入脚本配合使用。你可以从http://blog.deconcept.com/flashobject/ *下载FlashObject * 用法: * var ExpressInstall = new ExpressInstall(); * * // 检测是否需要快速安装: * if (ExpressInstall.needsUpdate) { // 需要快速安装返回true * ExpressInstall.init(); // 开始升级 * } * // 这个动作可以加到按钮上或者其他事件上激活 * // 或者在影片一开始就调用 * * 注意: 你的影片尺寸要大于 214px × 137px 才能使用快速安装.这是因为flash的安全策略所致,详情参见Macromedia * */ var ExpressInstall = function():Void { // 用户是否需要升级? this.needsUpdate = (_root.MMplayerType == undefined) ? false : true; } ExpressInstall.prototype = { init: function():Boolean { if (this.needsUpdate) { this.loadUpdater(); return true; } else { return false; } }, loadUpdater: function():Void { System.security.allowDomain(“fpdownload.macromedia.com”); // 希望你没在深度为10000000的层上方东西,你也可以自定义深度,只要保证在所有内容的最上就好 this.up&#100;ater = _root.cr&#101;ateEmptyMovieClip(“expressInstallHolder”, 10000000); //注册回调这样我们可以知道是取消了还是有错误 var _self = this; this.up&#100;ater.installStatus = _self.onInstallStatus; this.hold = this.up&#100;ater.cr&#101;ateEmptyMovieClip(“hold”, 1); // 不能使用 movieClipLoader 因为只有在 6.0.65 可以使用 this.up&#100;ater.onEnterFrame = function():Void { if (typeof this.hold.startUpdate == ‘function’) { _self.initUpdater(); this.onEnterFrame = null; } } var cacheBuster:Number = Math.random(); this.hold.loadMovie(“http://fpdownload.macromedia.com/pub/flashplayer/up&#100;ate/current/swf/autoUpdater.swf?”+ cacheBuster); }, initUpdater: function():Void { this.hold.redirectURL = _root.MMredirectURL; this.hold.MMplayerType = _root.MMplayerType; this.hold.MMdoctitle = _root.MMdoctitle; this.hold.startUpdate(); }, onInstallStatus: function(msg):Void { if (msg == “Download.Complete”) { // 安装完成. 大多数情况下窗口会被安装程序关闭或者被用户手动关闭 } else if (msg == “Download.Cancelled”) { // 用户选择了不升级,默认情况下没有给用户提供任何界面。这要求开发人员为用户提供其他的用户体验。 // 在这里一个JS错误提示就足够了 getURL(“javascript:alert(’需要最新的播放器支持.’)”); } else if (msg == “Download.Failed”) { // 最终用户遇到了网络问题升级错误,默认情况下没有给用户提供任何界面。这要求开发人员为用户提供其他的用户体验。 // 在这里一个JS错误提示就足够了 getURL(“javascript:alert(’下载失败.请稍后再试, 或者直接访问macromedia.com下载.’)”); } } } flash可以增加的属性可以参考 Flash OBJECT and EMBED 标[/code]