JavaScript fetching flash objects is a little different from the web

  • 2020-03-30 02:39:53
  • OfStack

There are a lot of examples on the web of js getting flash objects, and I've tried a lot.
It all worked, but there was nothing I wanted the most.
Later I looked at baidu, although it is very standard, all kinds of circumstances are considered, but the code is not optimistic,
Nearly 20 lines of code before and after, and 864 bytes after compression.

So this paper was born.
I'd like to test and share some of my favorite methods collected online.
Also by the way, please help to test the compatibility, there are incompatibilities in the following message, it is best to write the browser version, so I can also test and repair the code.
Let's look at the code.
function getFlashMovieObject( movieName ) { 
if (window.document[movieName]) { 
return window.document[movieName]; 
if (navigator.appName.indexOf("Microsoft Internet") == -1) { 
if (document.embeds && document.embeds[movieName]) 
return document.embeds[movieName]; 
} else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1) 
return document.getElementById(movieName); 

This method is conservative. If the value is found in the document, it will be returned directly. If it is not found, the author often USES embeds compatible substitution to fetch,
Of course, IE is special, but here authors use getElementById, while other places use window more often.
Of course, getElementById can't be wrong, he took the ID of object, because embeds has only name and no ID.
var flash = document["myFlash"] || window["myFlash"]; 

This method is more concise and clear, and I won't go into details.
var flash = document["myFlash"]; 

This is I looked at a variety of versions, wrote their own, because my computer various browsers + virtual machine ie6-10 has been tested, I also feel puzzled.
Of course, if I use it formally, I will still choose option 1, but in some special cases, if you don't need to be compatible with multiple browsers, you can consider this.

The following is a practical example of how to get flash objects in three different ways and test the flash control.
If some friends find that the test cannot pass, please leave the browser version number and the scheme that cannot pass, thanks in advance.

Related articles: