flashobject.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * FlashObject embed
  3. * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
  4. *
  5. * v1.1.1 - 05-17-2005
  6. *
  7. * writes the embed code for a flash movie, includes plugin detection
  8. *
  9. * Usage:
  10. *
  11. * myFlash = new FlashObject("path/to/swf.swf", "swfid", "width", "height", flashversion, "backgroundcolor");
  12. * myFlash.write("objId");
  13. *
  14. * for best practices, see:
  15. * http://blog.deconcept.com/2005/03/31/proper-flash-embedding-flashobject-best-practices/
  16. *
  17. */
  18. var FlashObject = function(swf, id, w, h, ver, c) {
  19. this.swf = swf;
  20. this.id = id;
  21. this.width = w;
  22. this.height = h;
  23. this.version = ver;
  24. this.align = "middle";
  25. this.params = new Object();
  26. this.variables = new Object();
  27. this.redirect = "";
  28. this.sq = document.location.search.split("?")[1] || "";
  29. this.bypassTxt = "<p>Already have Macromedia Flash Player? <a href='?detectflash=false&"+ this.sq +"'>Click here if you have Flash Player "+ this.version +" installed</a>.</p>";
  30. if (c) this.color = this.addParam('bgcolor', c);
  31. this.addParam('quality', 'high'); // default to high
  32. this.doDetect = getQueryParamValue('detectflash');
  33. }
  34. var FOP = FlashObject.prototype;
  35. FOP.addParam = function(name, value) { this.params[name] = value; }
  36. FOP.getParams = function() { return this.params; }
  37. FOP.getParam = function(name) { return this.params[name]; }
  38. FOP.addVariable = function(name, value) { this.variables[name] = value; }
  39. FOP.getVariable = function(name) { return this.variables[name]; }
  40. FOP.getVariables = function() { return this.variables; }
  41. FOP.getParamTags = function() {
  42. var paramTags = "";
  43. for (var param in this.getParams()) {
  44. paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
  45. }
  46. return (paramTags == "") ? false:paramTags;
  47. }
  48. FOP.getHTML = function() {
  49. var flashHTML = "";
  50. if (navigator.plugins && navigator.mimeTypes.length) { // netscape plugin architecture
  51. flashHTML += '<embed type="application/x-shockwave-flash" src="' + this.swf + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '"';
  52. for (var param in this.getParams()) {
  53. flashHTML += ' ' + param + '="' + this.getParam(param) + '"';
  54. }
  55. if (this.getVariablePairs()) {
  56. flashHTML += ' flashVars="' + this.getVariablePairs() + '"';
  57. }
  58. flashHTML += '></embed>';
  59. } else { // PC IE
  60. flashHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.width + '" height="' + this.height + '" name="' + this.id + '" align="' + this.align + '">';
  61. flashHTML += '<param name="movie" value="' + this.swf + '" />';
  62. if (this.getParamTags()) {
  63. flashHTML += this.getParamTags();
  64. }
  65. if (this.getVariablePairs() != null) {
  66. flashHTML += '<param name="flashVars" value="' + this.getVariablePairs() + '" />';
  67. }
  68. flashHTML += '</object>';
  69. }
  70. return flashHTML;
  71. }
  72. FOP.getVariablePairs = function() {
  73. var variablePairs = new Array();
  74. for (var name in this.getVariables()) {
  75. variablePairs.push(name + "=" + escape(this.getVariable(name)));
  76. }
  77. return (variablePairs.length > 0) ? variablePairs.join("&"):false;
  78. }
  79. FOP.write = function(elementId) {
  80. if(detectFlash(this.version) || this.doDetect=='false') {
  81. if (elementId) {
  82. document.getElementById(elementId).innerHTML = this.getHTML();
  83. } else {
  84. document.write(this.getHTML());
  85. }
  86. } else {
  87. if (this.redirect != "") {
  88. document.location.replace(this.redirect);
  89. } else if (this.altTxt) {
  90. if (elementId) {
  91. document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
  92. } else {
  93. document.write(this.altTxt +""+ this.bypassTxt);
  94. }
  95. }
  96. }
  97. }
  98. /* ---- detection functions ---- */
  99. function getFlashVersion() {
  100. var flashversion = 0;
  101. if (navigator.plugins && navigator.mimeTypes.length) {
  102. var x = navigator.plugins["Shockwave Flash"];
  103. if(x && x.description) {
  104. var y = x.description;
  105. flashversion = y.charAt(y.indexOf('.')-1);
  106. var aux= y.charAt(y.indexOf('.')-2);
  107. if("0123456789".indexOf(aux)!=-1) flashversion=aux+flashversion;
  108. }
  109. } else {
  110. result = false;
  111. for(var i = 15; i >= 3 && result != true; i--){
  112. execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
  113. flashversion = i;
  114. }
  115. }
  116. return flashversion;
  117. }
  118. function detectFlash(ver) { return (getFlashVersion() >= ver) ? true:false; }
  119. // get value of query string param
  120. function getQueryParamValue(param) {
  121. var q = document.location.search || document.location.href.split("#")[1];
  122. if (q) {
  123. var detectIndex = q.indexOf(param +"=");
  124. var endIndex = (q.indexOf("&", detectIndex) > -1) ? q.indexOf("&", detectIndex) : q.length;
  125. if (q.length > 1 && detectIndex > -1) {
  126. return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
  127. } else {
  128. return "";
  129. }
  130. }
  131. }
  132. /* add Array.push if needed */
  133. if(Array.prototype.push == null){
  134. Array.prototype.push = function(item) { this[this.length] = item; return this.length; }
  135. }