简体   繁体   中英

How to detect when a Flash movie gets focus in Javascript code?

Is it possible to create a such event? If yes, how?

The markup for a flash movie is something like the next. Without changing ActionScript code of flash movie.

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="32" height="32">
    <param name="movie" value="file://test.swf">
    <param name="quality" value="high">
    <embed src="file://test.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="32" height="32">
    </embed>
</object>

Yes, you can do so in JavaScript.

First, it is a good idea to use swfobject to handle cross browser Flash issues. This is a JavaScript library to control SWF objects. You can download it here: https://code.google.com/p/swfobject/downloads/list

Basic Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <title>SWFObject - low level dynamic publishing example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="swfobject.js"></script>
    <script type="text/javascript">
    if (swfobject.hasFlashPlayerVersion("6.0.0")) {
      var fn = function() {
        var att = { data:"test.swf", width:"780", height:"400" };
        var par = { flashvars:"foo=bar" };
        var id = "replaceMe";
        var myObject = swfobject.createSWF(att, par, id);
      };
      swfobject.addDomLoadEvent(fn);
    }
    </script>
  </head>
  <body>
    <div id="replaceMe">Alternative content</div>
  </body>
</html>

Next, just expand the example with an id inside the att variable like so:

var att = { data:"test.swf", width:"780", height:"400", id:"myId" };

You can access the object with getElementById() or a jQuery selector $("#myId")

Then attach an event to on focus like so:

JQuery: (recommended)

$('#myId').focus(function() {
  alert('SWF is in focus');
});

or

Standard:

object.onfocus=function(){ alert('SWF is in focus'); }

You can place the player in a div, and select the div using javascipt. Also there is a more info here: https://github.com/englandrp/Cross-browser-Flash-tabbing-and-focus-solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM