Skip to content

Instantly share code, notes, and snippets.

@dexteryy
Created December 22, 2011 05:34
Show Gist options
  • Save dexteryy/1509073 to your computer and use it in GitHub Desktop.
Save dexteryy/1509073 to your computer and use it in GitHub Desktop.
/**
* TUI::utilities::flash
* @created: Dexter.Yy
* @modified: $Author$
* @version: $Rev$
*/
/**
* @public 获取指定的flash对象
* @note 用于调用flash里提供的方法
* @param {string} m是flash元素的id
* @requires jQuery.browser
*/
TUI.getFlashMC = function(m){
return $.browser.msie ? window[m] : document[m];
};
/**
* 加载swf的通用方法
*/
TUI.swfobject = (function($, TUI){
var isie = !(navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length);
var webkit = TUI.browser.webkit;
var template = {
'object': '<object id="<%=id%>" <%=classid%> width="<%=width%>" height="<%=height%>" name="<%=name%>" <%=data%> style="<%=style%>" <%=mimetype%> >',
'embed': '<embed id="<%=id%>" width="<%=width%>" height="<%=height%>" flashvars="<%=flashvars%>" quality="high" name="<%=name%>" src="<%=src%>" style="<%=style%>" <%=mimetype%> '
};
var FLASH_MIME_TYPE = "application/x-shockwave-flash";
var Swfobj = {
/**
* public 嵌入flash标签
* @param {String} * objid是用来替换的页面元素的id,在没有id的时候使用document.write
*/
load: function(objid){
var box = objid ? document.getElementById(objid) : false;
if (box) { //用flash标签替换指定id的页面元素
if (box.outerHTML) { // for ie
box.outerHTML = this.getHTML();
} else {
var me = this, f = TUI.addElm(this.getHTML(), function(){
box.parentNode.replaceChild(this, box);
//this.setAttribute("data", me.attr.src); //防止重复加载 @TODO 此方法无效
});
}
} else { //在没有提供替换的页面元素时,在load方法执行的地方直接写入flash标签
document.write(this.getHTML());
}
},
/**
* 针对ie或其他浏览器生成相应的flash标签的html代码
*/
getHTML: function() {
var h = [], vars = [];
if (webkit && webkit < 312 || !isie && this.type === "embed") {
for (var n in this.flashvars)
vars.push(n + "=" + this.flashvars[n]);
this.attr.flashvars = vars.join("&");
this.attr.mimetype = 'type="' + FLASH_MIME_TYPE + '"';
h.push(TUI.renderTpl(template['embed'], this.attr));
for (var k in this.params)
h.push(k + '="' + this.params[k] + '" ');
h.push(' ></embed>');
} else {
if (isie) {
this.attr.classid = 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
} else {
this.attr.data = 'data="' + this.attr.src + '"';
this.attr.mimetype = 'type="' + FLASH_MIME_TYPE + '"';
}
h.push(TUI.renderTpl(template['object'], this.attr));
if(isie)
this.params["movie"] = this.attr.src;
this.params["quality"] = "high";
for (var k in this.params)
h.push('<param name="', k, '" value="', this.params[k], '" />');
for (var n in this.flashvars)
vars.push(n + "=" + this.flashvars[n]);
if (vars.length > 0)
h.push('<param name="flashvars" value="', vars.join("&"), '" />');
h.push("</object>");
}
return h.join("");
}
};
/**
* @factory 初始化flash对象
* @param {Object} url是swf地址
* @param {Object} w是flash宽度
* @param {Object} h是flash高度
* @param {Object} vars是flashvars属性
* @param {Object} params是flash元素里的param标签
* @param {Object} attrs是flash标签本身的属性
*/
return function(url,w,h,vars,params,attrs){
attrs = $.extend({
id: 'playerObject' + +new Date()
}, attrs || {});
if (!attrs.name)
attrs.name = attrs.id;
var tagType = attrs.tagType;
delete attrs.tagType;
return $.extend({
type: tagType,
attr: $.extend(attrs, {
src: url,
width: w,
height: h
}),
params: params || {},
flashvars: vars || {}
}, Swfobj);
};
})(jQuery, TUI);
/**
* @static 获取flashplayer的版本
* @return {Array}
*/
TUI.swfobject.getVersion = function() {
var ver = [0,0,0];
if (navigator.plugins && navigator.mimeTypes.length) {
var x = navigator.plugins["Shockwave Flash"];
if (x && x.description) //"10.0 r115", "10.0 d51", "10.0 b51"
ver = x.description.replace(/^\D+/, '').replace(/\s*r/, '.').replace(/\s*[a-z]+\d*/, '.0').split('.');
} else {
if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0) {
var axo = 1;
var n = 3;
while (axo) {
try {
n++;
axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + n);
ver = [n, 0, 0];
} catch(e) {
axo = null;
}
}
} else {
try {
var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
} catch(e) {
try {
var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
ver = [6, 0, 21];
axo.AllowScriptAccess = "always";
} catch(e) {
if (ver.major == 6)
return ver;
}
try {
axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
} catch(e) {}
}
if (axo != null) {
ver = axo.GetVariable("$version").split(" ")[1].split(",");
}
}
}
return ver;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment