Flash控件的自动激活

1. 在中间加入objectSwap.js
<script src='objectSwap.js' type='text/javascript'>
2. falsh控件部分 to the object tag
具体参考见:http://blog.deconcept.com/swfobject/

object代码如下:

js 代码
  1. /* ObjectSwap - Bypasses the new ActiveX Activation requirement in Internet Explorer by swapping existing ActiveX objects on the page with the same objects. Can also be used for Flash version detection by adding the param:  
  2. <param name="flashVersion" value="8" /> to the object tag.  
  3.  
  4. Author: Karina Steffens, www.neo-archaic.net  
  5. Created: April 2006  
  6. Changes and bug fixes: May 2006  
  7. Bug fixes: June 2006  
  8. Changes: October 2006 (Included Opera9 and excluded IE5.5)  
  9. */  
  10.   
  11. //Check if the browser is InternetExplorer, and if it supports the getElementById DOM method   
  12. var ie = (document.defaultCharset && document.getElementById && !window.home);   
  13. var opera9 = false;   
  14. if (ie){   
  15.     //Check for ie 5.5 and exclude it from the script   
  16.     var ver=navigator.appVersion.split("MSIE")   
  17.     ver=parseFloat(ver[1])   
  18.     ie = (ver >=6)   
  19. }else if (navigator.userAgent.indexOf("Opera")!=-1) {   
  20.     //Check for Opera9 and include it in the ObjectSwap   
  21.     var versionindex=navigator.userAgent.indexOf("Opera")+6   
  22.     if (parseInt(navigator.userAgent.charAt(versionindex))>=9)   
  23.     opera9 = true;   
  24. }   
  25. //Perform ObjectSwap if the browser is IE or Opera (if not just check flashVersion)   
  26. var oswap = (ie || opera9)   
  27.   
  28. //Hide the object to prevent it from loading twice   
  29. if (oswap){   
  30.     document.write ("<style id='hideObject'> object{display:none;} </style>");   
  31. }   
  32.   
  33. /*Replace all flash objects on the page with the same flash object,   
  34. by rewriting the outerHTML values  
  35. This bypasses the new IE ActiveX object activation issue*/  
  36. objectSwap = function(){   
  37.     if (!document.getElementsByTagName){   
  38.         return;   
  39.     }   
  40.     //An array of ids for flash detection   
  41.     var stripQueue = [];   
  42.     //Get a list of all ActiveX objects   
  43.     var objects = document.getElementsByTagName('object');   
  44.     for (var i=0; i<objects.length; i++){              
  45.         var o = objects[i];    
  46.         var h = o.outerHTML;   
  47.         //The outer html omits the param tags, so we must retrieve and insert these separately   
  48.         var params = "";   
  49.         var hasFlash = true;   
  50.         for (var j = 0; j<o.childNodes.length; j++) {   
  51.             var p = o.childNodes[j];   
  52.             if (p.tagName == "PARAM"){   
  53.                 //Check for version first - applies to all browsers   
  54.                 //For this to work, a new param needs to be included in the object with the name "flashVersion" eg:   
  55.                 //<param name="flashVersion" value="7" />   
  56.                 if (p.name == "flashVersion"){   
  57.                     hasFlash = detectFlash(p.value);   
  58.                     if (!hasFlash){   
  59.                         //Add the objects id to the list (create a new id if there's isn't one already)   
  60.                         o.id = (o.id == "") ? ("stripFlash"+i) : o.id;   
  61.                         stripQueue.push(o.id);   
  62.                         break;   
  63.                     }   
  64.                 }    
  65.                 params += p.outerHTML;                
  66.             }   
  67.         }      
  68.         if (!hasFlash){   
  69.             continue;   
  70.         }          
  71.         //Only target internet explorer   
  72.         if (!oswap){   
  73.             continue;   
  74.         }    
  75.         //Avoid specified objects, marked with a "noswap" classname   
  76.         if (o.className.toLowerCase().indexOf ("noswap") != -1){   
  77.             continue;   
  78.         }          
  79.         //Get the tag and attributes part of the outer html of the object   
  80.         var tag = h.split(">")[0] + ">";               
  81.         //Add up the various bits that comprise the object:   
  82.         //The tag with the attributes, the params and it's inner html   
  83.         var newObject = tag + params + o.innerHTML + " </OBJECT>";     
  84.         //And rewrite the outer html of the tag    
  85.         o.outerHTML = newObject;   
  86.     }   
  87.     //Strip flash objects   
  88.     if (stripQueue.length) {   
  89.         stripFlash(stripQueue)   
  90.     }   
  91.     //Make the objects visible again   
  92.     if (oswap){   
  93.         document.getElementById("hideObject").disabled = true;   
  94.     }   
  95. }   
  96.   
  97. detectFlash = function(version){   
  98.     if(navigator.plugins && navigator.plugins.length){   
  99.         //Non-IE flash detection.   
  100.         var plugin = navigator.plugins["Shockwave Flash"];   
  101.         if (plugin == undefined){   
  102.             return false;   
  103.         }   
  104.         var ver = navigator.plugins["Shockwave Flash"].description.split(" ")[2];   
  105.         return (Number(ver) >= Number(version))   
  106.     } else if (ie && typeof (ActiveXObject) == "function"){   
  107.     //IE flash detection.   
  108.         try{   
  109.             var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + version);   
  110.             return true;   
  111.         }   
  112.         catch(e){   
  113.             return false;   
  114.         }   
  115.     }   
  116.     //Catchall - skip detection   
  117.     return true;   
  118. }   
  119.   
  120. //Loop through an array of ids to strip   
  121. //Replace the object by a div tag containing the same innerHTML.   
  122. //To display an alternative image, message for the user or a link to the flash installation page, place it inside the object tag.     
  123. //For the usual object/embed pairs it needs to be enclosed in comments to hide from gecko based browsers.   
  124. stripFlash = function (stripQueue){   
  125.     if (!document.createElement){   
  126.         return;   
  127.     }   
  128.     for (var i=0; i<stripQueue.length; i++){   
  129.         var o = document.getElementById(stripQueue[i]);   
  130.         var newHTML = o.innerHTML;     
  131.         //Strip the comments   
  132.         newHTML = newHTML.replace(/<!--\s/g, "");   
  133.         newHTML = newHTML.replace(/\s-->/g, "");   
  134.         //Neutralise the embed tag   
  135.         newHTML = newHTML.replace(/<embed/gi, "<span");        
  136.         //Create a new div element with properties from the object   
  137.         var d = document.createElement("div");   
  138.         d.innerHTML = newHTML;   
  139.         d.className = o.className;   
  140.         d.id = o.id;   
  141.         //And swap the object with the new div   
  142.         o.parentNode.replaceChild(d, o);   
  143.     }   
  144. }   
  145.   
  146. //Initiate the function without conflicting with the window.onload event of any preceding scripts   
  147. var tempFunc = window.onload;   
  148. window.onload = function(){   
  149.     if (typeof (tempFunc) == "function"){   
  150.         try{   
  151.             tempFunc();   
  152.         } catch(e){}   
  153.     }   
  154.     objectSwap();   
  155. }   
  156.   

你可能感兴趣的:(JavaScript,Opera,IE,Flash,J#)