Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript to Detect Flash Plugin

Status
Not open for further replies.

caffeinerusher

Programmer
Mar 7, 2001
31
US
Hi,

I need help with some javascript that will detect a flash plugin. If the browser has the plugin, it needs to show the flash, if it does not, it needs to show a static image. I can only seem to get the script to partially work. I need it to work on IE 4.0+ Netscape 4.0+ for both PC and MAC.
Any help would be appreciated.

--Caffeinerusher
 
The javascript navigator.plugins.length only works in NetScape. IE4 and later do actually support this javascript but returns an empty array. IE only supports this code for compatibility reasons. So I don't think you can detect plugins in both NS and IE. Klae

You're only as good as your last answer!
 
Here you go, works with both Netscape and IE, in IE it uses VBscript to detect the plugins via activex.


Tim

Code:
<html>
<SCRIPT language=Javascript>
<!-- hide script
/*
Plugin Detector()
*/

var WM_startTagFix = '</';
var msie_windows = 0;
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)){
  msie_windows = 1;
  document.writeln('<script language=&quot;VBscript&quot;>');
  document.writeln('\'This will scan for plugins for all versions of Internet Explorer that have a VBscript engine version 2 or greater.');
  document.writeln('\'This includes all versions of IE4 and beyond and some versions of IE 3.');
  document.writeln('Dim WM_detect_through_vb');
  document.writeln('WM_detect_through_vb = 0');
  document.writeln('If ScriptEngineMajorVersion >= 2 then');
  document.writeln('  WM_detect_through_vb = 1');
  document.writeln('End If');
  document.writeln('Function WM_activeXDetect(activeXname)');
  document.writeln('  on error resume next');
  document.writeln('  If ScriptEngineMajorVersion >= 2 then');
  document.writeln('     WM_activeXDetect = False');
  document.writeln('     WM_activeXDetect = IsObject(CreateObject(activeXname))');
  document.writeln('     If (err) then');
  document.writeln('        WM_activeXDetect = False');
  document.writeln('     End If');
  document.writeln('   Else');
  document.writeln('     WM_activeXDetect = False');
  document.writeln('   End If');
  document.writeln('End Function');
  document.writeln(WM_startTagFix+'script>');
}

function WM_pluginDetect(plugindescription, pluginxtension, pluginmime, activeXname){
  var i,plugin_undetectable=0,detected=0, daPlugin=new Object();

  if (msie_windows && WM_detect_through_vb){
      plugin_undetectable = 0;
  } else {
      plugin_undetectable = 1;
  }

  if(navigator.plugins) {
      numPlugins = navigator.plugins.length;
      if (numPlugins > 1) {
	  if (navigator.mimeTypes && navigator.mimeTypes[pluginmime] && navigator.mimeTypes[pluginmime].enabledPlugin && (navigator.mimeTypes[pluginmime].suffixes.indexOf(pluginxtension) != -1)) { // seems like we have it, let's just make sure and check the version (if specified)
	      if ((navigator.appName == 'Netscape') && (navigator.appVersion.indexOf('4.0') != -1)) { // Netscape can't handle the references to navigator.plugins by number
		  for(i in navigator.plugins) {
		      if ((navigator.plugins[i].description.indexOf(plugindescription) != -1) || (i.indexOf(plugindescription) != -1)) { // some versions of quicktime have no description. 
			  detected=1;
			  break;
		      }
		  }
	      } else {
		  for (i = 0; i < numPlugins; i++) {
		      daPlugin = navigator.plugins[i];
		      if ((daPlugin.description.indexOf(plugindescription) != -1) || (daPlugin.name.indexOf(plugindescription) != -1)) {
			  detected=1;
			  break;
		      }
		  }
	      }

	      // Mac weirdness
      if (navigator.mimeTypes[pluginmime] == null) {
		  detected = 0;
	      }
	  }
	  return detected;
      } else if((msie_windows == 1) && !plugin_undetectable){
	  return WM_activeXDetect(activeXname);
      } else { 		
	  return 0;
      }
  } else {
      return 0;
  }
}


function WM_easyDetect(whichPlugin) {
    var isItThere = 0;
    if( (whichPlugin == 'flash') || (whichPlugin == 'Flash') ) {
	isItThere = WM_pluginDetect('Flash', 'swf', 'application/x-shockwave-flash', 'ShockwaveFlash.ShockwaveFlash');
    } 
    return isItThere;
}

// end hiding of script --> 
</SCRIPT>
      <script>
<!--

if (WM_easyDetect('flash') == 0)
{
  document.write(&quot;<font size=2 color=#0D499A><b>Flash</b> is not supported you can download it here<br><br>&quot;+ 
  &quot;<a href=[URL unfurl="true"]http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash><img[/URL] border=0 src=BrowserImg/shockwave.gif></a><p>&quot;);
}
else
{
document.write(&quot;<b><font size=2 color=#0D499A>Flash:</b> Supported</font><br><br>\n\n&quot;);
}


</script>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top