1 /*! SWFObject v2.1 <http://code.google.com/p/swfobject/>
2 Copyright (c) 2007-2008 Geoff Stearns, Michael Williams, and Bobby van der Sluis
3 This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
6 var swfobject = function() {
8 var UNDEF = "undefined",
10 SHOCKWAVE_FLASH = "Shockwave Flash",
11 SHOCKWAVE_FLASH_AX = "ShockwaveFlash.ShockwaveFlash",
12 FLASH_MIME_TYPE = "application/x-shockwave-flash",
13 EXPRESS_INSTALL_ID = "SWFObjectExprInst",
25 storedAltContent = null,
26 storedAltContentId = null,
28 isExpressInstallActive = false;
30 /* Centralized function for browser feature detection
31 - Proprietary feature detection (conditional compiling) is used to detect Internet Explorer's features
32 - User agent string detection is only used when no alternative is possible
33 - Is executed directly for optimal performance
36 var w3cdom = typeof doc.getElementById != UNDEF && typeof doc.getElementsByTagName != UNDEF && typeof doc.createElement != UNDEF,
37 playerVersion = [0,0,0],
39 if (typeof nav.plugins != UNDEF && typeof nav.plugins[SHOCKWAVE_FLASH] == OBJECT) {
40 d = nav.plugins[SHOCKWAVE_FLASH].description;
41 if (d && !(typeof nav.mimeTypes != UNDEF && nav.mimeTypes[FLASH_MIME_TYPE] && !nav.mimeTypes[FLASH_MIME_TYPE].enabledPlugin)) { // navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin indicates whether plug-ins are enabled or disabled in Safari 3+
42 d = d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
43 playerVersion[0] = parseInt(d.replace(/^(.*)\..*$/, "$1"), 10);
44 playerVersion[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
45 playerVersion[2] = /r/.test(d) ? parseInt(d.replace(/^.*r(.*)$/, "$1"), 10) : 0;
48 else if (typeof win.ActiveXObject != UNDEF) {
49 var a = null, fp6Crash = false;
51 a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".7");
55 a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".6");
56 playerVersion = [6,0,21];
57 a.AllowScriptAccess = "always"; // Introduced in fp6.0.47
60 if (playerVersion[0] == 6) {
66 a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
71 if (!fp6Crash && a) { // a will return null when ActiveX is disabled
73 d = a.GetVariable("$version"); // Will crash fp6.0.21/23/29
75 d = d.split(" ")[1].split(",");
76 playerVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
82 var u = nav.userAgent.toLowerCase(),
83 p = nav.platform.toLowerCase(),
84 webkit = /webkit/.test(u) ? parseFloat(u.replace(/^.*webkit\/(\d+(\.\d+)?).*$/, "$1")) : false, // returns either the webkit version or false if not webkit
86 windows = p ? /win/.test(p) : /win/.test(u),
87 mac = p ? /mac/.test(p) : /mac/.test(u);
96 return { w3cdom:w3cdom, pv:playerVersion, webkit:webkit, ie:ie, win:windows, mac:mac };
99 /* Cross-browser onDomLoad
100 - Based on Dean Edwards' solution: http://dean.edwards.name/weblog/2006/06/again/
101 - Will fire an event as soon as the DOM of a page is loaded (supported by Gecko based browsers - like Firefox -, IE, Opera9+, Safari)
103 var onDomLoad = function() {
107 addDomLoadEvent(main);
108 if (ua.ie && ua.win) {
109 try { // Avoid a possible Operation Aborted error
110 doc.write("<scr" + "ipt id=__ie_ondomload defer=true src=//:></scr" + "ipt>"); // String is split into pieces to avoid Norton AV to add code that can cause errors
111 script = getElementById("__ie_ondomload");
113 addListener(script, "onreadystatechange", checkReadyState);
118 if (ua.webkit && typeof doc.readyState != UNDEF) {
119 timer = setInterval(function() { if (/loaded|complete/.test(doc.readyState)) { callDomLoadFunctions(); }}, 10);
121 if (typeof doc.addEventListener != UNDEF) {
122 doc.addEventListener("DOMContentLoaded", callDomLoadFunctions, null);
124 addLoadEvent(callDomLoadFunctions);
127 function checkReadyState() {
128 if (script.readyState == "complete") {
129 script.parentNode.removeChild(script);
130 callDomLoadFunctions();
134 function callDomLoadFunctions() {
138 if (ua.ie && ua.win) { // Test if we can really add elements to the DOM; we don't want to fire it too early
139 var s = createElement("span");
140 try { // Avoid a possible Operation Aborted error
141 var t = doc.getElementsByTagName("body")[0].appendChild(s);
142 t.parentNode.removeChild(t);
150 clearInterval(timer);
153 var dl = domLoadFnArr.length;
154 for (var i = 0; i < dl; i++) {
159 function addDomLoadEvent(fn) {
164 domLoadFnArr[domLoadFnArr.length] = fn; // Array.push() is only available in IE5.5+
168 /* Cross-browser onload
169 - Based on James Edwards' solution: http://brothercake.com/site/resources/scripts/onload/
170 - Will fire an event as soon as a web page including all of its assets are loaded
172 function addLoadEvent(fn) {
173 if (typeof win.addEventListener != UNDEF) {
174 win.addEventListener("load", fn, false);
176 else if (typeof doc.addEventListener != UNDEF) {
177 doc.addEventListener("load", fn, false);
179 else if (typeof win.attachEvent != UNDEF) {
180 addListener(win, "onload", fn);
182 else if (typeof win.onload == "function") {
183 var fnOld = win.onload;
184 win.onload = function() {
195 - Will preferably execute onDomLoad, otherwise onload (as a fallback)
197 function main() { // Static publishing only
198 var rl = regObjArr.length;
199 for (var i = 0; i < rl; i++) { // For each registered object element
200 var id = regObjArr[i].id;
202 var obj = getElementById(id);
204 regObjArr[i].width = obj.getAttribute("width") ? obj.getAttribute("width") : "0";
205 regObjArr[i].height = obj.getAttribute("height") ? obj.getAttribute("height") : "0";
206 if (hasPlayerVersion(regObjArr[i].swfVersion)) { // Flash plug-in version >= Flash content version: Houston, we have a match!
207 if (ua.webkit && ua.webkit < 312) { // Older webkit engines ignore the object element's nested param elements
210 setVisibility(id, true);
212 else if (regObjArr[i].expressInstall && !isExpressInstallActive && hasPlayerVersion("6.0.65") && (ua.win || ua.mac)) { // Show the Adobe Express Install dialog if set by the web page author and if supported (fp6.0.65+ on Win/Mac OS only)
213 showExpressInstall(regObjArr[i]);
215 else { // Flash plug-in and Flash content version mismatch: display alternative content instead of Flash content
216 displayAltContent(obj);
220 else { // If no fp is installed, we let the object element do its job (show alternative content)
221 setVisibility(id, true);
226 /* Fix nested param elements, which are ignored by older webkit engines
227 - This includes Safari up to and including version 1.2.2 on Mac OS 10.3
228 - Fall back to the proprietary embed element
230 function fixParams(obj) {
231 var nestedObj = obj.getElementsByTagName(OBJECT)[0];
233 var e = createElement("embed"), a = nestedObj.attributes;
236 for (var i = 0; i < al; i++) {
237 if (a[i].nodeName == "DATA") {
238 e.setAttribute("src", a[i].nodeValue);
241 e.setAttribute(a[i].nodeName, a[i].nodeValue);
245 var c = nestedObj.childNodes;
248 for (var j = 0; j < cl; j++) {
249 if (c[j].nodeType == 1 && c[j].nodeName == "PARAM") {
250 e.setAttribute(c[j].getAttribute("name"), c[j].getAttribute("value"));
254 obj.parentNode.replaceChild(e, obj);
258 /* Show the Adobe Express Install dialog
259 - Reference: http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=6a253b75
261 function showExpressInstall(regObj) {
262 isExpressInstallActive = true;
263 var obj = getElementById(regObj.id);
265 if (regObj.altContentId) {
266 var ac = getElementById(regObj.altContentId);
268 storedAltContent = ac;
269 storedAltContentId = regObj.altContentId;
273 storedAltContent = abstractAltContent(obj);
275 if (!(/%$/.test(regObj.width)) && parseInt(regObj.width, 10) < 310) {
276 regObj.width = "310";
278 if (!(/%$/.test(regObj.height)) && parseInt(regObj.height, 10) < 137) {
279 regObj.height = "137";
281 doc.title = doc.title.slice(0, 47) + " - Flash Player Installation";
282 var pt = ua.ie && ua.win ? "ActiveX" : "PlugIn",
284 fv = "MMredirectURL=" + win.location + "&MMplayerType=" + pt + "&MMdoctitle=" + dt,
285 replaceId = regObj.id;
286 // For IE when a SWF is loading (AND: not available in cache) wait for the onload event to fire to remove the original object element
287 // In IE you cannot properly cancel a loading SWF file without breaking browser load references, also obj.onreadystatechange doesn't work
288 if (ua.ie && ua.win && obj.readyState != 4) {
289 var newObj = createElement("div");
290 replaceId += "SWFObjectNew";
291 newObj.setAttribute("id", replaceId);
292 obj.parentNode.insertBefore(newObj, obj); // Insert placeholder div that will be replaced by the object element that loads expressinstall.swf
293 obj.style.display = "none";
294 var fn = function() {
295 obj.parentNode.removeChild(obj);
297 addListener(win, "onload", fn);
299 createSWF({ data:regObj.expressInstall, id:EXPRESS_INSTALL_ID, width:regObj.width, height:regObj.height }, { flashvars:fv }, replaceId);
303 /* Functions to abstract and display alternative content
305 function displayAltContent(obj) {
306 if (ua.ie && ua.win && obj.readyState != 4) {
307 // For IE when a SWF is loading (AND: not available in cache) wait for the onload event to fire to remove the original object element
308 // In IE you cannot properly cancel a loading SWF file without breaking browser load references, also obj.onreadystatechange doesn't work
309 var el = createElement("div");
310 obj.parentNode.insertBefore(el, obj); // Insert placeholder div that will be replaced by the alternative content
311 el.parentNode.replaceChild(abstractAltContent(obj), el);
312 obj.style.display = "none";
313 var fn = function() {
314 obj.parentNode.removeChild(obj);
316 addListener(win, "onload", fn);
319 obj.parentNode.replaceChild(abstractAltContent(obj), obj);
323 function abstractAltContent(obj) {
324 var ac = createElement("div");
325 if (ua.win && ua.ie) {
326 ac.innerHTML = obj.innerHTML;
329 var nestedObj = obj.getElementsByTagName(OBJECT)[0];
331 var c = nestedObj.childNodes;
334 for (var i = 0; i < cl; i++) {
335 if (!(c[i].nodeType == 1 && c[i].nodeName == "PARAM") && !(c[i].nodeType == 8)) {
336 ac.appendChild(c[i].cloneNode(true));
345 /* Cross-browser dynamic SWF creation
347 function createSWF(attObj, parObj, id) {
348 var r, el = getElementById(id);
350 if (typeof attObj.id == UNDEF) { // if no 'id' is defined for the object element, it will inherit the 'id' from the alternative content
353 if (ua.ie && ua.win) { // IE, the object element and W3C DOM methods do not combine: fall back to outerHTML
355 for (var i in attObj) {
356 if (attObj[i] != Object.prototype[i]) { // Filter out prototype additions from other potential libraries, like Object.prototype.toJSONString = function() {}
357 if (i.toLowerCase() == "data") {
358 parObj.movie = attObj[i];
360 else if (i.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
361 att += ' class="' + attObj[i] + '"';
363 else if (i.toLowerCase() != "classid") {
364 att += ' ' + i + '="' + attObj[i] + '"';
369 for (var j in parObj) {
370 if (parObj[j] != Object.prototype[j]) { // Filter out prototype additions from other potential libraries
371 par += '<param name="' + j + '" value="' + parObj[j] + '" />';
374 el.outerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + att + '>' + par + '</object>';
375 objIdArr[objIdArr.length] = attObj.id; // Stored to fix object 'leaks' on unload (dynamic publishing only)
376 r = getElementById(attObj.id);
378 else if (ua.webkit && ua.webkit < 312) { // Older webkit engines ignore the object element's nested param elements: fall back to the proprietary embed element
379 var e = createElement("embed");
380 e.setAttribute("type", FLASH_MIME_TYPE);
381 for (var k in attObj) {
382 if (attObj[k] != Object.prototype[k]) { // Filter out prototype additions from other potential libraries
383 if (k.toLowerCase() == "data") {
384 e.setAttribute("src", attObj[k]);
386 else if (k.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
387 e.setAttribute("class", attObj[k]);
389 else if (k.toLowerCase() != "classid") { // Filter out IE specific attribute
390 e.setAttribute(k, attObj[k]);
394 for (var l in parObj) {
395 if (parObj[l] != Object.prototype[l]) { // Filter out prototype additions from other potential libraries
396 if (l.toLowerCase() != "movie") { // Filter out IE specific param element
397 e.setAttribute(l, parObj[l]);
401 el.parentNode.replaceChild(e, el);
404 else { // Well-behaving browsers
405 var o = createElement(OBJECT);
406 o.setAttribute("type", FLASH_MIME_TYPE);
407 for (var m in attObj) {
408 if (attObj[m] != Object.prototype[m]) { // Filter out prototype additions from other potential libraries
409 if (m.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
410 o.setAttribute("class", attObj[m]);
412 else if (m.toLowerCase() != "classid") { // Filter out IE specific attribute
413 o.setAttribute(m, attObj[m]);
417 for (var n in parObj) {
418 if (parObj[n] != Object.prototype[n] && n.toLowerCase() != "movie") { // Filter out prototype additions from other potential libraries and IE specific param element
419 createObjParam(o, n, parObj[n]);
422 el.parentNode.replaceChild(o, el);
429 function createObjParam(el, pName, pValue) {
430 var p = createElement("param");
431 p.setAttribute("name", pName);
432 p.setAttribute("value", pValue);
436 /* Cross-browser SWF removal
437 - Especially needed to safely and completely remove a SWF in Internet Explorer
439 function removeSWF(id) {
440 var obj = getElementById(id);
441 if (obj && (obj.nodeName == "OBJECT" || obj.nodeName == "EMBED")) {
442 if (ua.ie && ua.win) {
443 if (obj.readyState == 4) {
444 removeObjectInIE(id);
447 win.attachEvent("onload", function() {
448 removeObjectInIE(id);
453 obj.parentNode.removeChild(obj);
458 function removeObjectInIE(id) {
459 var obj = getElementById(id);
462 if (typeof obj[i] == "function") {
466 obj.parentNode.removeChild(obj);
470 /* Functions to optimize JavaScript compression
472 function getElementById(id) {
475 el = doc.getElementById(id);
481 function createElement(el) {
482 return doc.createElement(el);
485 /* Updated attachEvent function for Internet Explorer
486 - Stores attachEvent information in an Array, so on unload the detachEvent functions can be called to avoid memory leaks
488 function addListener(target, eventType, fn) {
489 target.attachEvent(eventType, fn);
490 listenersArr[listenersArr.length] = [target, eventType, fn];
493 /* Flash Player and SWF content version matching
495 function hasPlayerVersion(rv) {
496 var pv = ua.pv, v = rv.split(".");
497 v[0] = parseInt(v[0], 10);
498 v[1] = parseInt(v[1], 10) || 0; // supports short notation, e.g. "9" instead of "9.0.0"
499 v[2] = parseInt(v[2], 10) || 0;
500 return (pv[0] > v[0] || (pv[0] == v[0] && pv[1] > v[1]) || (pv[0] == v[0] && pv[1] == v[1] && pv[2] >= v[2])) ? true : false;
503 /* Cross-browser dynamic CSS creation
504 - Based on Bobby van der Sluis' solution: http://www.bobbyvandersluis.com/articles/dynamicCSS.php
506 function createCSS(sel, decl) {
507 if (ua.ie && ua.mac) {
510 var h = doc.getElementsByTagName("head")[0], s = createElement("style");
511 s.setAttribute("type", "text/css");
512 s.setAttribute("media", "screen");
513 if (!(ua.ie && ua.win) && typeof doc.createTextNode != UNDEF) {
514 s.appendChild(doc.createTextNode(sel + " {" + decl + "}"));
517 if (ua.ie && ua.win && typeof doc.styleSheets != UNDEF && doc.styleSheets.length > 0) {
518 var ls = doc.styleSheets[doc.styleSheets.length - 1];
519 if (typeof ls.addRule == OBJECT) {
520 ls.addRule(sel, decl);
525 function setVisibility(id, isVisible) {
526 var v = isVisible ? "visible" : "hidden";
527 if (isDomLoaded && getElementById(id)) {
528 getElementById(id).style.visibility = v;
531 createCSS("#" + id, "visibility:" + v);
535 /* Filter to avoid XSS attacks
537 function urlEncodeIfNecessary(s) {
538 var regex = /[\\\"<>\.;]/;
539 var hasBadChars = regex.exec(s) != null;
540 return hasBadChars ? encodeURIComponent(s) : s;
543 /* Release memory to avoid memory leaks caused by closures, fix hanging audio/video threads and force open sockets/NetConnections to disconnect (Internet Explorer only)
545 var cleanup = function() {
546 if (ua.ie && ua.win) {
547 window.attachEvent("onunload", function() {
548 // remove listeners to avoid memory leaks
549 var ll = listenersArr.length;
550 for (var i = 0; i < ll; i++) {
551 listenersArr[i][0].detachEvent(listenersArr[i][1], listenersArr[i][2]);
553 // cleanup dynamically embedded objects to fix audio/video threads and force open sockets and NetConnections to disconnect
554 var il = objIdArr.length;
555 for (var j = 0; j < il; j++) {
556 removeSWF(objIdArr[j]);
558 // cleanup library's main closures to avoid memory leaks
563 for (var l in swfobject) {
574 - Reference: http://code.google.com/p/swfobject/wiki/SWFObject_2_0_documentation
576 registerObject: function(objectIdStr, swfVersionStr, xiSwfUrlStr) {
577 if (!ua.w3cdom || !objectIdStr || !swfVersionStr) {
581 regObj.id = objectIdStr;
582 regObj.swfVersion = swfVersionStr;
583 regObj.expressInstall = xiSwfUrlStr ? xiSwfUrlStr : false;
584 regObjArr[regObjArr.length] = regObj;
585 setVisibility(objectIdStr, false);
588 getObjectById: function(objectIdStr) {
591 var o = getElementById(objectIdStr);
593 var n = o.getElementsByTagName(OBJECT)[0];
594 if (!n || (n && typeof o.SetVariable != UNDEF)) {
597 else if (typeof n.SetVariable != UNDEF) {
605 embedSWF: function(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj) {
606 if (!ua.w3cdom || !swfUrlStr || !replaceElemIdStr || !widthStr || !heightStr || !swfVersionStr) {
609 widthStr += ""; // Auto-convert to string
611 if (hasPlayerVersion(swfVersionStr)) {
612 setVisibility(replaceElemIdStr, false);
614 if (attObj && typeof attObj === OBJECT) {
615 for (var i in attObj) {
616 if (attObj[i] != Object.prototype[i]) { // Filter out prototype additions from other potential libraries
621 att.data = swfUrlStr;
622 att.width = widthStr;
623 att.height = heightStr;
625 if (parObj && typeof parObj === OBJECT) {
626 for (var j in parObj) {
627 if (parObj[j] != Object.prototype[j]) { // Filter out prototype additions from other potential libraries
632 if (flashvarsObj && typeof flashvarsObj === OBJECT) {
633 for (var k in flashvarsObj) {
634 if (flashvarsObj[k] != Object.prototype[k]) { // Filter out prototype additions from other potential libraries
635 if (typeof par.flashvars != UNDEF) {
636 par.flashvars += "&" + k + "=" + flashvarsObj[k];
639 par.flashvars = k + "=" + flashvarsObj[k];
644 addDomLoadEvent(function() {
645 createSWF(att, par, replaceElemIdStr);
646 if (att.id == replaceElemIdStr) {
647 setVisibility(replaceElemIdStr, true);
651 else if (xiSwfUrlStr && !isExpressInstallActive && hasPlayerVersion("6.0.65") && (ua.win || ua.mac)) {
652 isExpressInstallActive = true; // deferred execution
653 setVisibility(replaceElemIdStr, false);
654 addDomLoadEvent(function() {
656 regObj.id = regObj.altContentId = replaceElemIdStr;
657 regObj.width = widthStr;
658 regObj.height = heightStr;
659 regObj.expressInstall = xiSwfUrlStr;
660 showExpressInstall(regObj);
665 getFlashPlayerVersion: function() {
666 return { major:ua.pv[0], minor:ua.pv[1], release:ua.pv[2] };
669 hasFlashPlayerVersion: hasPlayerVersion,
671 createSWF: function(attObj, parObj, replaceElemIdStr) {
673 return createSWF(attObj, parObj, replaceElemIdStr);
680 removeSWF: function(objElemIdStr) {
682 removeSWF(objElemIdStr);
686 createCSS: function(sel, decl) {
688 createCSS(sel, decl);
692 addDomLoadEvent: addDomLoadEvent,
694 addLoadEvent: addLoadEvent,
696 getQueryParamValue: function(param) {
697 var q = doc.location.search || doc.location.hash;
699 return urlEncodeIfNecessary(q);
702 var pairs = q.substring(1).split("&");
703 for (var i = 0; i < pairs.length; i++) {
704 if (pairs[i].substring(0, pairs[i].indexOf("=")) == param) {
705 return urlEncodeIfNecessary(pairs[i].substring((pairs[i].indexOf("=") + 1)));
712 // For internal usage only
713 expressInstallCallback: function() {
714 if (isExpressInstallActive && storedAltContent) {
715 var obj = getElementById(EXPRESS_INSTALL_ID);
717 obj.parentNode.replaceChild(storedAltContent, obj);
718 if (storedAltContentId) {
719 setVisibility(storedAltContentId, true);
720 if (ua.ie && ua.win) {
721 storedAltContent.style.display = "block";
724 storedAltContent = null;
725 storedAltContentId = null;
726 isExpressInstallActive = false;