June 11, 2001 - Supporting Multiple Flash Objects | WebReference

June 11, 2001 - Supporting Multiple Flash Objects

Yehuda Shiran June 11, 2001
Supporting Multiple Flash Objects
Tips: June 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

In some of your applications you may need to use Flash movies and JavaScript directly, without any prepackaged APIs such as FlashSound JavaScript API. You will enjoy more features, methods, and properties, but you'll have to take care of all the tiny details that are taken for granted when using FlashSound API, for example.

One of the "tiny" details is embedding the Flash object. The JavaScript file flashcheck.js does exactly this, as well as other stuff. For example, it checks whether a Flash player exists (Flash plug-in or ActiveX control), and whether its version is higher than the minimum required by the application.

The JavaScript include file flashcheck.js is different from the one we used so far, flashmoviecheck.js, in that it supports multiple Flash objects. The file flashmoviecheck.js includes a fixed ID (for Internet Explorer) and a fixed NAME (for Netscape Navigator). Both are set to "sonify". Setting a fixed object prevents us from setting two or more Flash objects on the same page. The file flashcheck.js answers this problem by letting you pass the ID/NAME as the second parameter to Flash_embedSWF(). In this way you can call Flash_embedSWF() as many times as you want on the same page. The fundamental change is to replace the fixed string "sonify" with a parameter. In document.writeln statements, this is straightforward. In other JavaScript statements, we need to use the eval() function to concatenate between fixed strings and a parameter. Here is flashcheck.js:

winIEpass = ((navigator.appName.indexOf("Microsoft") != -1) && 
  (navigator.appVersion.indexOf("Windows") != -1)) && 
  (parseFloat(navigator.appVersion) >= 4) ? true : false;
NNpass = ((navigator.appName == "Netscape") && 
  (navigator.userAgent.indexOf("Mozilla") != -1) && 
  (parseFloat(navigator.appVersion) >= 4) && 
  (navigator.javaEnabled())) ? true : false;
  supportedBrowser = (winIEpass || NNpass) ? true : false;
// check for Flash Plug-in in Mac or Win Navigator. Get plug-in version.
minPlayer = 4;
var mySwf;
function Flash_checkForPlugIn() {
  var plugin = (navigator.mimeTypes &&
  navigator.mimeTypes["application/x-shockwave-flash"]) ?
  navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
  if (plugin) {
    var pluginversion = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1)) 
    if(pluginversion >= minPlayer) {return true;}
  }
  return false;
}
// vbscript check for Flash ActiveX control in windows IE
if(supportedBrowser && winIEpass) {
  document.write(
    '<script language=VBScript>' + '\n' +
    'Function Flash_checkForActiveX()' + '\n' +
    'Dim hasPlayer, playerversion' + '\n' +
    'hasPlayer = false' + '\n' +
    'playerversion = 10' + '\n' +
    'Do While playerversion >= minPlayer' + '\n' +
    'On Error Resume Next' + '\n' +
    'hasPlayer = (IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" & playerversion & \"\")))' + '\n' +
    'If hasPlayer = true Then Exit Do' + '\n' +
    'playerversion = playerversion - 1' + '\n' +
    'Loop' + '\n' +
    'Flash_checkForActiveX = hasPlayer' + '\n' +
    'End Function' + '\n' +
    '<\/script>'
  );
}
function Flash_checkForMinPlayer() {
  if(!supportedBrowser) return false;
  if(NNpass) return (Flash_checkForPlugIn());
  if(winIEpass) return (Flash_checkForActiveX());
}
function Flash_embedSWF(srcURL, id, swfbgColor) {
  if (!Flash_checkForMinPlayer()) return;
  var defaultColor = (document.bgColor != null) ? document.bgColor : "#ffffff";
  var bgcolor = (swfbgColor != null) ? swfbgColor : defaultColor;
  document.writeln(
    '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0"' +
    'ID="' + id + '" WIDTH="100%" HEIGHT="100%">' +
    '<PARAM NAME="movie" VALUE="' + srcURL + '">' + 
    '<PARAM NAME="quality" VALUE="high">' +
    '<PARAM NAME="wmode" VALUE="transparent">'+
    '<PARAM NAME="autostart" VALUE="false">'+
    '<PARAM NAME="bgcolor" VALUE=' + bgcolor + '>' +
    '<embed src="' + srcURL + '" quality="high"' + 'bgcolor="' + 
    bgcolor + '"' + 'width="100%" height="100%"' +
    'type="application/x-shockwave-flash" NAME="' + id + '"' +
 'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></OBJECT>');
  eval('window.document.'+ id + '.Rewind()');
}
function sonified_flash(id, myFrame){
  if(!Flash_checkForMinPlayer()) {return;}
  mySwf = eval('window.document.' + id);
  if (mySwf.PercentLoaded()<100) return
  mySwf.GotoFrame(myFrame);
  mySwf.GotoFrame(0);
}