home *** CD-ROM | disk | FTP | other *** search
/ Yamaha Educational Products 2002 / YAMAHA_INSTRUMENTS_CD_2002.iso / pc / detect / Dispatcher.js next >
Encoding:
JavaScript  |  2002-01-01  |  16.4 KB  |  587 lines

  1. /*********************************************************************
  2.  *
  3.  * Macromedia Flash Dispatcher -- a scriptable detector for Flash Player
  4.  *
  5.  *
  6.  * copyright (c) 2000 Macromedia, Inc.
  7.  *
  8.  *********************************************************************/
  9.  
  10.  
  11. /*
  12.  * URL of the Flash self-detecting movie ("sniffer").
  13.  *
  14.  * Reset this if you move the file out of the directory in which the
  15.  * document containing the script that calls MM_FlashDispatch() resides.
  16.  */
  17.  
  18. var MM_FlashSnifferURL = "detectFlash.swf";
  19.  
  20.  
  21. /*
  22.  * Latest available revisions of the Plug-in.
  23.  */
  24.  
  25. var MM_latestPluginRevision = new Object();
  26. MM_latestPluginRevision["5.0"] = new Object();
  27. MM_latestPluginRevision["4.0"] = new Object();
  28. MM_latestPluginRevision["3.0"] = new Object();
  29. MM_latestPluginRevision["2.0"] = new Object();
  30.  
  31. /*
  32.  * This table must be updated as new versions and revisions of the
  33.  * plug-in are released, in support of the 'requireLatestRevision'
  34.  * option in MM_FlashDispatch().
  35.  */
  36.  
  37. MM_latestPluginRevision["5.0"]["Windows"] = 30;
  38. MM_latestPluginRevision["5.0"]["Macintosh"] = 30;
  39.  
  40. MM_latestPluginRevision["4.0"]["Windows"] = 28;
  41. MM_latestPluginRevision["4.0"]["Macintosh"] = 27;
  42. MM_latestPluginRevision["4.0"]["Unix"] = 12;
  43.  
  44. MM_latestPluginRevision["3.0"]["Windows"] = 10;
  45. MM_latestPluginRevision["3.0"]["Macintosh"] = 10;
  46.  
  47. MM_latestPluginRevision["2.0"]["Windows"] = 11;
  48. MM_latestPluginRevision["2.0"]["Macintosh"] = 11;
  49.  
  50.  
  51. /*
  52.  * MM_FlashInfo() -- construct an object representing Flash Player status
  53.  *
  54.  * Constructor:
  55.  *
  56.  *    new MM_FlashInfo()
  57.  *
  58.  * Properties:
  59.  *
  60.  *    installed        true if player is installed
  61.  *                (undefined if undetectable)
  62.  *
  63.  *    implementation        the form the player takes in this
  64.  *                browser: "ActiveX control" or "Plug-in"
  65.  *
  66.  *    autoInstallable        true if the player can be automatically
  67.  *                installed/updated on this browser/platform
  68.  *
  69.  *    version            player version if installed
  70.  *
  71.  *    revision        revision if implementation is "Plug-in"
  72.  *
  73.  * Methods:
  74.  *
  75.  *    canPlay(contentVersion)    true if installed player is capable of
  76.  *                playing content authored with the
  77.  *                specified version of Flash software
  78.  *
  79.  * Description:
  80.  *
  81.  *    MM_FlashInfo() instantiates an object that contains as much
  82.  *    information about Flash Player--whether it is installed, what
  83.  *    version is installed, and so one--as is possible to collect.
  84.  *
  85.  *    Where Flash Player is implemented as a plug-in and the user's
  86.  *    browser supports plug-in detection, all properties are defined;
  87.  *    this includes Netscape on all platforms and Microsoft Internet
  88.  *    Explorer 5 on the Macintosh.  Where Flash Player is implemented
  89.  *    as an ActiveX control (MSIE on Windows), all properties except
  90.  *    'revision' are defined.
  91.  *
  92.  *    Prior to version 5, Microsoft Internet Explorer on the Macintosh
  93.  *    did not support plug-in detection.  In this case, no properties
  94.  *    are defined, unless the cookie 'MM_FlashDetectedSelf' has been
  95.  *    set, in which case all properties except 'version' and 'revision'
  96.  *    are set.
  97.  *
  98.  *    This object is primarily meant for use by MM_FlashDispatch(), but
  99.  *    may be of use in reporting the player version, etc. to the user.
  100.  */
  101.  
  102. var MM_FlashControlInstalled;    // is the Flash ActiveX control installed?
  103. var MM_FlashControlVersion;    // ActiveX control version if installed
  104.  
  105. function MM_FlashInfo()
  106. {
  107.     if (navigator.plugins && navigator.plugins.length > 0)
  108.     {
  109.     this.implementation = "Plug-in";
  110.     this.autoInstallable = false;    // until Netscape SmartUpdate supported
  111.  
  112.     // Check whether the plug-in is installed:
  113.  
  114.     if (navigator.plugins["Shockwave Flash"])
  115.     {
  116.         this.installed = true;
  117.  
  118.         // Get the plug-in version and revision:
  119.  
  120.         var words =
  121.         navigator.plugins["Shockwave Flash"].description.split(" ");
  122.  
  123.         for (var i = 0; i < words.length; ++i)
  124.         {
  125.         if (isNaN(parseInt(words[i])))
  126.         continue;
  127.  
  128.         this.version = words[i];
  129.  
  130.         this.revision = parseInt(words[i + 1].substring(1));
  131.         }
  132.     }
  133.     else
  134.     {
  135.         this.installed = false;
  136.     }
  137.     }
  138.     else if (MM_FlashControlInstalled != null)
  139.     {
  140.     this.implementation = "ActiveX control";
  141.     this.installed = MM_FlashControlInstalled;
  142.     this.version = MM_FlashControlVersion;
  143.     this.autoInstallable = true;
  144.     }
  145.     else if (MM_FlashDetectedSelf())
  146.     {
  147.     this.installed = true;
  148.     this.implementation = "Plug-in";
  149.     this.autoInstallable = false;
  150.     }
  151.  
  152.     this.canPlay = MM_FlashCanPlay;
  153. }
  154.  
  155.  
  156. /*
  157.  * MM_FlashDispatch() -- get Flash Player status and redirect appropriately
  158.  *
  159.  * Synopsis:
  160.  *
  161.  *    MM_FlashDispatch(contentURL, contentVersion, requireLatestRevision,
  162.  *             upgradeURL, install, installURL, altURL,
  163.  *             overridePluginsPage)
  164.  *
  165.  *    Arguments:
  166.  *
  167.  *        contentURL            URL of document containing Flash content
  168.  *
  169.  *        contentVersion        version of Flash software used to
  170.  *                    author content
  171.  *
  172.  *        requireLatestRevision    Boolean indicating whether to require
  173.  *                    latest revision of player (plug-in only)
  174.  *
  175.  *        upgradeURL            document to load if player must be
  176.  *                    upgraded to play content and automated
  177.  *                    updating is not supported on the user's
  178.  *                    browser & platform
  179.  *
  180.  *        install            Boolean indicating whether to install
  181.  *                    if player is not installed
  182.  *
  183.  *        installURL            document to load if 'install' is true
  184.  *                    and automated installation is not
  185.  *                    supported on user's browser & platform
  186.  *
  187.  *        altURL            document to load if 'install' is false
  188.  *
  189.  *        overridePluginsPage        Boolean indicating whether to set the
  190.  *                    PLUGINSPAGE attribute for the embedded
  191.  *                    Flash Player sniffer to `installURL'
  192.  *
  193.  *    Returns:
  194.  *
  195.  *        Normally, never returns; changes window.location.
  196.  *        Returns with no value when called improperly.
  197.  *
  198.  * Description:
  199.  *
  200.  *    MM_FlashDispatch() detects whether the user's Web browser has the
  201.  *    Flash plug-in or ActiveX control installed, and what version is
  202.  *    installed if so. It then takes appropriate action based on whether
  203.  *    Flash Player is installed and is compatible with 'contentVersion':
  204.  *    load a document containing Flash content, load alternate content,
  205.  *    or oversee the updating or installation of the player.
  206.  *
  207.  *    There are three possible outcomes of the detection process: 
  208.  *
  209.  *        1. A version of Flash Player has been detected that is
  210.  *           suitable for playing the requested content version.
  211.  *           MM_FlashDispatch() will load 'contentURL'.
  212.  *
  213.  *        2. An unsuitable version of Flash Player has been detected.
  214.  *           MM_FlashDispatch() will load 'contentURL' if automated
  215.  *           updating is supported on the user's browser & platform;
  216.  *           otherwise, it will load 'upgradeURL'.
  217.  *
  218.  *        3. Flash Player is not installed.  If 'install' is set to
  219.  *           true, MM_FlashDispatch() will load 'contentURL' if the
  220.  *           user's browser supports automated installation; otherwise,
  221.  *           it will load 'installURL'.  If 'install' is false,
  222.  *           MM_FlashDispatch() will load 'altURL'.
  223.  *
  224.  *    When script-based detection of Flash Player is not possible,
  225.  *    MM_FlashDispatch() attempts to load a Flash movie to carry out
  226.  *    the detection. If Flash Player is not installed, there is presently
  227.  *    no choice but to let the browser redirect the user via the
  228.  *    PLUGINSPAGE attribute of the EMBED tag. In this case, 'install'
  229.  *    is ignored, but setting 'overridePluginsPage' to true will
  230.  *    set PLUGINSPAGE to 'installURL', overriding its default value
  231.  *    (the URL for the Macromedia Flash download center). If this flag
  232.  *    is set, 'installURL' must be absolute, not relative.
  233.  */
  234.  
  235. var MM_FlashPluginsPage = "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash";
  236.  
  237. function MM_FlashDispatch(contentURL, contentVersion, requireLatestRevision,
  238.               upgradeURL, install, installURL, altURL,
  239.               overridePluginsPage)
  240. {
  241.     if (overridePluginsPage == null)
  242.     {
  243.     alert("ERROR: MM_FlashDispatch() called with too few arguments.");
  244.     return;
  245.     }
  246.  
  247.  
  248.     if (overridePluginsPage && installURL.substring(0, 7) != "http://")
  249.     {
  250.     alert("ERROR: MM_FlashDispatch() called with relative URL" +
  251.         " for PLUGINSPAGE (" + installURL + ")");
  252.  
  253.     return;
  254.     }
  255.  
  256.  
  257.     var player = new MM_FlashInfo();
  258.  
  259.     if (player.installed == null)
  260.     {
  261.     var sniffer =
  262.         "<EMBED HIDDEN=\"true\" TYPE=\"application/x-shockwave-flash\"" +
  263.           " WIDTH=\"18\" HEIGHT=\"18\"" +
  264.           " BGCOLOR=\"" + document.bgcolor + "\"" +
  265.           " SRC=\"" + MM_FlashSnifferURL +
  266.             "?contentURL=" + contentURL + "?" +
  267.             "&contentVersion=" + contentVersion +
  268.             "&requireLatestRevision=" + requireLatestRevision +
  269.             "&latestRevision=" +
  270.                 MM_FlashLatestPluginRevision(contentVersion) +
  271.             "&upgradeURL=" + upgradeURL +
  272.             "\"" +
  273.           " LOOP=\"false\" MENU=\"false\"" +
  274.           " PLUGINSPAGE=\"" +
  275.             (overridePluginsPage ? installURL : MM_FlashPluginsPage) +
  276.             "\"" +
  277.         ">" +
  278.         "</EMBED>";
  279.  
  280.     document.open();
  281.     document.write("<HTML><HEAD><TITLE>");
  282.     document.write("Checking for the Flash Player");
  283.     document.write("</TITLE></HEAD>");
  284.     document.write("<BODY BGCOLOR=\"" + document.bgcolor + "\">");
  285.     document.write(sniffer);
  286.     document.write("</BODY>");
  287.     document.write("</HTML>");
  288.     document.close();
  289.     }
  290.     else if (player.installed)
  291.     {
  292.     if (player.canPlay(contentVersion, requireLatestRevision))
  293.     {
  294.         location = contentURL;
  295.     }
  296.     else
  297.     {
  298.         location = player.autoInstallable ? contentURL : upgradeURL;
  299.     }
  300.     }
  301.     else if (install)
  302.     {
  303.     location = player.autoInstallable ? contentURL : installURL;
  304.     }
  305.     else
  306.     {
  307.     location = altURL;
  308.     }
  309. }
  310.  
  311.  
  312. /*
  313.  * MM_FlashRememberIfDetectedSelf() -- record that Flash Player detected itself
  314.  *
  315.  * Synopsis:
  316.  *
  317.  *    MM_FlashRememberIfDetectedSelf()
  318.  *    MM_FlashRememberIfDetectedSelf(count)
  319.  *    MM_FlashRememberIfDetectedSelf(count, units)
  320.  *
  321.  *    Arguments:
  322.  *
  323.  *        count        length of time in units before re-checking
  324.  *                whether content can be played (default: 60)
  325.  *
  326.  *        units        unit(s) of time to count: "minute(s)," "hour(s)"
  327.  *                 or "day(s)" (default: "days")
  328.  *
  329.  *
  330.  * Description:
  331.  *
  332.  *    This function conditionally sets a cookie signifying that
  333.  *    the current document was referred via the Dispatcher using
  334.  *    Flash Player self-detection.  It is intended to spare the user
  335.  *    whose browser does not support script-based detection from the
  336.  *    process of Flash Player self-detection on each visit.
  337.  *    
  338.  *    The cookie persists for 60 days, or for the amount of time
  339.  *    specified by the 'count' and 'units' parameters.
  340.  *
  341.  *    If cookies are not being accepted, this function is a no-op;
  342.  *    the Dispatcher will simply attempt Flash Player self-detection
  343.  *    on subsequent visits.
  344.  *
  345.  *    This function must be called from a script embedded in the
  346.  *    document referenced by the 'contentURL' argument to
  347.  *    MM_FlashDispatch().
  348.  *
  349.  */
  350.  
  351. function MM_FlashRememberIfDetectedSelf(count, units)
  352. {
  353.     // the sniffer appends an empty search string to the URL
  354.     // to indicate that it is the referrer
  355.  
  356.     if (document.location.search.indexOf("?") != -1)
  357.     {
  358.     if (!count) count = 60;
  359.     if (!units) units = "days";
  360.  
  361.     var msecs = new Object();
  362.  
  363.     msecs.minute = msecs.minutes = 60000;
  364.     msecs.hour = msecs.hours = 60 * msecs.minute;
  365.     msecs.day = msecs.days = 24 * msecs.hour;
  366.  
  367.     var expires = new Date();
  368.  
  369.     expires.setTime(expires.getTime() + count * msecs[units]);
  370.  
  371.     document.cookie =
  372.         'MM_FlashDetectedSelf=true ; expires=' + expires.toGMTString();
  373.     }
  374. }
  375.  
  376.  
  377. /*
  378.  * MM_FlashDemur() -- record user's decision not to install Flash Player
  379.  *
  380.  * Synopsis:
  381.  *
  382.  *    MM_FlashDemur()
  383.  *    MM_FlashDemur(count)
  384.  *    MM_FlashDemur(count, units)
  385.  *
  386.  *    Arguments:
  387.  *
  388.  *        count    length of time in units to remember decision
  389.  *            (default: 60)
  390.  *
  391.  *        units    unit(s) of time to count: "minute(s)," "hour(s)"
  392.  *            or "day(s)" (default: "days")
  393.  *
  394.  *    Returns:
  395.  *
  396.  *        true if successful; false otherwise.
  397.  *
  398.  * Description:
  399.  *
  400.  *    MM_FlashDemur() sets a cookie signifying that the user requested
  401.  *    that the decision not to install Flash be remembered.
  402.  *
  403.  *    The cookie persists for 60 days, or for the amount of time
  404.  *    specified by the 'count' and 'units' parameters.
  405.  *
  406.  *    This function may be used as the handler for the 'onClick' event
  407.  *    associated with the user's selecting a link to alternate content.
  408.  *    If cookies are not being accepted, it will return false; this
  409.  *    may be used to control whether the link is followed.
  410.  */
  411.  
  412. function MM_FlashDemur(count, units)
  413. {
  414.     if (!count) count = 60;
  415.     if (!units) units = "days";
  416.  
  417.     var msecs = new Object();
  418.  
  419.     msecs.minute = msecs.minutes = 60000;
  420.     msecs.hour = msecs.hours = 60 * msecs.minute;
  421.     msecs.day = msecs.days = 24 * msecs.hour;
  422.  
  423.     var expires = new Date();
  424.  
  425.     expires.setTime(expires.getTime() + count * msecs[units]);
  426.  
  427.     document.cookie =
  428.     'MM_FlashUserDemurred=true ; expires=' + expires.toGMTString();
  429.  
  430.  
  431.     if (!MM_FlashUserDemurred())
  432.     {
  433.     alert("Your browser must accept cookies in order to " +
  434.           "save this information.  Try changing your preferences.");
  435.  
  436.     return false;
  437.     }
  438.     else
  439.     return true;
  440. }
  441.  
  442.  
  443. /*
  444.  * MM_FlashUserDemurred() -- recall user's decision not to install Flash Player
  445.  *
  446.  * Synopsis:
  447.  *
  448.  *    MM_FlashUserDemurred()
  449.  *
  450.  *    Returns:
  451.  *
  452.  *        true if a cookie signifying that the user declined to install
  453.  *        Flash Player is set; false otherwise.
  454.  *
  455.  * Description:
  456.  *
  457.  *    This function is useful in determining whether to set the 'install'
  458.  *    flag when calling MM_FlashDispatch().  If true, it means that the
  459.  *    user's previous decision not to install Flash Player should be
  460.  *    honored, i.e., 'install' should be set to false.
  461.  */
  462.  
  463. function MM_FlashUserDemurred()
  464. {
  465.     return (document.cookie.indexOf("MM_FlashUserDemurred") != -1);
  466. }
  467.  
  468.  
  469. /*********************************************************************
  470.  * THE FOLLOWING FUNCTIONS ARE NOT PUBLIC.  DO NOT CALL THEM DIRECTLY.
  471.  *********************************************************************/
  472.  
  473. /*
  474.  * MM_FlashLatestPluginRevision() -- look up latest Flash Player plug-in
  475.  *                     revision for this platform
  476.  *
  477.  * Synopsis:
  478.  *
  479.  *    MM_FlashLatestPluginRevision(playerVersion)
  480.  *
  481.  *    Arguments:
  482.  *
  483.  *        playerVersion    plug-in version to look up revision of
  484.  *
  485.  *    Returns:
  486.  *
  487.  *        The latest available revision of the specified version of
  488.  *        the Flash Player plug-in on this platform, as an integer;
  489.  *        undefined for versions before 2.0.
  490.  *
  491.  * Description:
  492.  *
  493.  *    This look-up function is only intended to be called internally.
  494.  */
  495.  
  496. function MM_FlashLatestPluginRevision(playerVersion)
  497. {
  498.     var latestRevision;
  499.     var platform;
  500.  
  501.     if (navigator.appVersion.indexOf("Win") != -1)
  502.     platform = "Windows";
  503.     else if (navigator.appVersion.indexOf("Macintosh") != -1)
  504.     platform = "Macintosh";
  505.     else if (navigator.appVersion.indexOf("X11") != -1)
  506.     platform = "Unix";
  507.  
  508.     latestRevision = MM_latestPluginRevision[playerVersion][platform];
  509.  
  510.     return latestRevision;
  511. }
  512.  
  513.  
  514. /*
  515.  * MM_FlashCanPlay() -- check whether installed Flash Player can play content
  516.  *
  517.  * Synopsis:
  518.  *
  519.  *    MM_FlashCanPlay(contentVersion, requireLatestRevision)
  520.  *
  521.  *    Arguments:
  522.  *
  523.  *        contentVersion        version of Flash software used to
  524.  *                    author content
  525.  *
  526.  *        requireLatestRevision    Boolean indicating whether latest
  527.  *                    revision of plug-in should be required
  528.  *
  529.  *    Returns:
  530.  *
  531.  *        true if the installed player can play the indicated content;
  532.  *        false otherwise.
  533.  *
  534.  * Description:
  535.  *
  536.  *    This function is not intended to be called directly, only
  537.  *    as an instance method of MM_FlashInfo.
  538.  */
  539.  
  540. function MM_FlashCanPlay(contentVersion, requireLatestRevision)
  541. {
  542.     var canPlay;
  543.  
  544.     if (this.version)
  545.     {
  546.     canPlay = (parseInt(contentVersion) <= this.version);
  547.  
  548.     if (requireLatestRevision)
  549.     {
  550.         if (this.revision &&
  551.         this.revision < MM_FlashLatestPluginRevision(this.version))
  552.         {
  553.         canPlay = false;
  554.         }
  555.     }
  556.     }
  557.     else
  558.     {
  559.     canPlay = MM_FlashDetectedSelf();
  560.     }
  561.  
  562.     return canPlay;
  563. }
  564.  
  565.  
  566. /*
  567.  * MM_FlashDetectedSelf() -- recall whether Flash Player has detected itself
  568.  *
  569.  * Synopsis:
  570.  *
  571.  *    MM_FlashDetectedSelf()
  572.  *
  573.  *    Returns:
  574.  *
  575.  *        true if a cookie signifying that Flash Player has detected itself
  576.  *        is set; false otherwise.
  577.  *
  578.  * Description:
  579.  *
  580.  *    This function is only meant to be called internally.
  581.  */
  582.  
  583. function MM_FlashDetectedSelf()
  584. {
  585.     return (document.cookie.indexOf("MM_FlashDetectedSelf") != -1);
  586. }
  587.