By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

Javascript Flash Detector

P: 1
Hey everyone, i've been playing around with trying to get some scripts to work with detecting whether flash player is installed on your computer or not. I found a flash detection kit on the adobe site which had a client side detection method using javascript.

Basically what im trying to get it to do is if flash player is installed, use the style property display="none" to hide my static content, and if flash is not installed hide the flash content using display="none".

Here's my page.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Flash Test</title>
  6. </head>
  7.     <script src="flash_check.js" language="javascript" type="text/javascript"></script>
  8.     <script language="JavaScript" type="text/javascript">
  9.         var requiredMajorVersion = 9;
  10.         var requiredMinorVersion = 0;
  11.         var requiredRevision = 0;
  12.     </script>
  13. <body>
  14.     <script language="JavaScript" type="text/javascript">
  15.         var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  16.         if (hasReqestedVersion) {
  17.             document.getElementById("html").style.display="none";
  18.         } else {
  19.             document.getElementById("flash").style.display="none";
  20.         }
  21.     </script>
  22.     <div id="html">
  23.         This is the Static Content
  24.         <br />
  25.     </div>
  26.     <div id="flash">
  27.         This is the Flash Content
  28.         <br />
  29.     </div>
  30. </body>
  31. </html>
  32.  
This is the js file that Adobe included with there detection pack. I haven't changed anything in this file.

Expand|Select|Wrap|Line Numbers
  1. // Flash Player Version Detection - Rev 1.5
  2. // Detect Client Browser type
  3. // Copyright(c) 2005-2006 Adobe Macromedia Software, LLC. All rights reserved.
  4. var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
  5. var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
  6. var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
  7.  
  8. function ControlVersion()
  9. {
  10.     var version;
  11.     var axo;
  12.     var e;
  13.  
  14.     // NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry
  15.  
  16.     try {
  17.         // version will be set for 7.X or greater players
  18.         axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
  19.         version = axo.GetVariable("$version");
  20.     } catch (e) {
  21.     }
  22.  
  23.     if (!version)
  24.     {
  25.         try {
  26.             // version will be set for 6.X players only
  27.             axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
  28.  
  29.             // installed player is some revision of 6.0
  30.             // GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
  31.             // so we have to be careful. 
  32.  
  33.             // default to the first public version
  34.             version = "WIN 6,0,21,0";
  35.  
  36.             // throws if AllowScripAccess does not exist (introduced in 6.0r47)        
  37.             axo.AllowScriptAccess = "always";
  38.  
  39.             // safe to call for 6.0r47 or greater
  40.             version = axo.GetVariable("$version");
  41.  
  42.         } catch (e) {
  43.         }
  44.     }
  45.  
  46.     if (!version)
  47.     {
  48.         try {
  49.             // version will be set for 4.X or 5.X player
  50.             axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
  51.             version = axo.GetVariable("$version");
  52.         } catch (e) {
  53.         }
  54.     }
  55.  
  56.     if (!version)
  57.     {
  58.         try {
  59.             // version will be set for 3.X player
  60.             axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
  61.             version = "WIN 3,0,18,0";
  62.         } catch (e) {
  63.         }
  64.     }
  65.  
  66.     if (!version)
  67.     {
  68.         try {
  69.             // version will be set for 2.X player
  70.             axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
  71.             version = "WIN 2,0,0,11";
  72.         } catch (e) {
  73.             version = -1;
  74.         }
  75.     }
  76.  
  77.     return version;
  78. }
  79.  
  80. // JavaScript helper required to detect Flash Player PlugIn version information
  81. function GetSwfVer(){
  82.     // NS/Opera version >= 3 check for Flash plugin in plugin array
  83.     var flashVer = -1;
  84.  
  85.     if (navigator.plugins != null && navigator.plugins.length > 0) {
  86.         if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
  87.             var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
  88.             var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;            
  89.             var descArray = flashDescription.split(" ");
  90.             var tempArrayMajor = descArray[2].split(".");
  91.             var versionMajor = tempArrayMajor[0];
  92.             var versionMinor = tempArrayMajor[1];
  93.             if ( descArray[3] != "" ) {
  94.                 tempArrayMinor = descArray[3].split("r");
  95.             } else {
  96.                 tempArrayMinor = descArray[4].split("r");
  97.             }
  98.             var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  99.             var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
  100.         }
  101.     }
  102.     // MSN/WebTV 2.6 supports Flash 4
  103.     else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
  104.     // WebTV 2.5 supports Flash 3
  105.     else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
  106.     // older WebTV supports Flash 2
  107.     else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
  108.     else if ( isIE && isWin && !isOpera ) {
  109.         flashVer = ControlVersion();
  110.     }    
  111.     return flashVer;
  112. }
  113.  
  114. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  115. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  116. {
  117.     versionStr = GetSwfVer();
  118.     if (versionStr == -1 ) {
  119.         return false;
  120.     } else if (versionStr != 0) {
  121.         if(isIE && isWin && !isOpera) {
  122.             // Given "WIN 2,0,0,11"
  123.             tempArray         = versionStr.split(" ");     // ["WIN", "2,0,0,11"]
  124.             tempString        = tempArray[1];            // "2,0,0,11"
  125.             versionArray      = tempString.split(",");    // ['2', '0', '0', '11']
  126.         } else {
  127.             versionArray      = versionStr.split(".");
  128.         }
  129.         var versionMajor      = versionArray[0];
  130.         var versionMinor      = versionArray[1];
  131.         var versionRevision   = versionArray[2];
  132.  
  133.             // is the major.revision >= requested major.revision AND the minor version >= requested minor
  134.         if (versionMajor > parseFloat(reqMajorVer)) {
  135.             return true;
  136.         } else if (versionMajor == parseFloat(reqMajorVer)) {
  137.             if (versionMinor > parseFloat(reqMinorVer))
  138.                 return true;
  139.             else if (versionMinor == parseFloat(reqMinorVer)) {
  140.                 if (versionRevision >= parseFloat(reqRevision))
  141.                     return true;
  142.             }
  143.         }
  144.         return false;
  145.     }
  146. }
  147.  
  148. function AC_AddExtension(src, ext)
  149. {
  150.   if (src.indexOf('?') != -1)
  151.     return src.replace(/\?/, ext+'?'); 
  152.   else
  153.     return src + ext;
  154. }
  155.  
  156. function AC_Generateobj(objAttrs, params, embedAttrs) 
  157.     var str = '';
  158.     if (isIE && isWin && !isOpera)
  159.     {
  160.           str += '<object ';
  161.           for (var i in objAttrs)
  162.               str += i + '="' + objAttrs[i] + '" ';
  163.           for (var i in params)
  164.              str += '><param name="' + i + '" value="' + params[i] + '" /> ';
  165.           str += '></object>';
  166.     } else {
  167.           str += '<embed ';
  168.           for (var i in embedAttrs)
  169.               str += i + '="' + embedAttrs[i] + '" ';
  170.           str += '> </embed>';
  171.     }
  172.     document.write(str);
  173.  
  174.  }
  175.  
  176. function AC_FL_RunContent(){
  177.   var ret = 
  178.     AC_GetArgs
  179.     (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
  180.      , "application/x-shockwave-flash"
  181.     );
  182.   AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
  183. }
  184.  
  185. function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  186.   var ret = new Object();
  187.   ret.embedAttrs = new Object();
  188.   ret.params = new Object();
  189.   ret.objAttrs = new Object();
  190.   for (var i=0; i < args.length; i=i+2){
  191.     var currArg = args[i].toLowerCase();    
  192.  
  193.     switch (currArg){    
  194.       case "classid":
  195.         break;
  196.       case "pluginspage":
  197.         ret.embedAttrs[args[i]] = args[i+1];
  198.         break;
  199.       case "src":
  200.       case "movie":    
  201.         args[i+1] = AC_AddExtension(args[i+1], ext);
  202.         ret.embedAttrs["src"] = args[i+1];
  203.         ret.params[srcParamName] = args[i+1];
  204.         break;
  205.       case "onafterupdate":
  206.       case "onbeforeupdate":
  207.       case "onblur":
  208.       case "oncellchange":
  209.       case "onclick":
  210.       case "ondblClick":
  211.       case "ondrag":
  212.       case "ondragend":
  213.       case "ondragenter":
  214.       case "ondragleave":
  215.       case "ondragover":
  216.       case "ondrop":
  217.       case "onfinish":
  218.       case "onfocus":
  219.       case "onhelp":
  220.       case "onmousedown":
  221.       case "onmouseup":
  222.       case "onmouseover":
  223.       case "onmousemove":
  224.       case "onmouseout":
  225.       case "onkeypress":
  226.       case "onkeydown":
  227.       case "onkeyup":
  228.       case "onload":
  229.       case "onlosecapture":
  230.       case "onpropertychange":
  231.       case "onreadystatechange":
  232.       case "onrowsdelete":
  233.       case "onrowenter":
  234.       case "onrowexit":
  235.       case "onrowsinserted":
  236.       case "onstart":
  237.       case "onscroll":
  238.       case "onbeforeeditfocus":
  239.       case "onactivate":
  240.       case "onbeforedeactivate":
  241.       case "ondeactivate":
  242.       case "type":
  243.       case "codebase":
  244.       case "id":
  245.         ret.objAttrs[args[i]] = args[i+1];
  246.         break;
  247.       case "width":
  248.       case "height":
  249.       case "align":
  250.       case "vspace": 
  251.       case "hspace":
  252.       case "class":
  253.       case "title":
  254.       case "accesskey":
  255.       case "name":
  256.       case "tabindex":
  257.         ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
  258.         break;
  259.       default:
  260.         ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
  261.     }
  262.   }
  263.   ret.objAttrs["classid"] = classid;
  264.   if (mimeType) ret.embedAttrs["type"] = mimeType;
  265.   return ret;
  266. }
  267.  
Any help would be appreaciated. :)
Mar 7 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Check that the Adobe detection script gives the correct version number, etc. or if uninstalled, what output is given. Based on that, you can use the script to show/hide content.
Mar 8 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.