Home  >  Article  >  Web Front-end  >  JavaScript obtains flash objects differently from those on the Internet_javascript skills

JavaScript obtains flash objects differently from those on the Internet_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:51:48967browse

Regarding js obtaining flash objects, there are many examples on the Internet, and I have also tried many methods.
Although they are all usable, they don’t have what I want most.
Later I looked at Baidu’s. Although it is very standardized and all situations are taken into consideration, the amount of code is not unoptimistic.
Before There are nearly 20 lines of code before and after, and there are 864 bytes after compression.

So this article was born.
I would like to test and share several methods that I am satisfied with collected from the Internet.
I would also like to ask everyone to help test the compatibility. If there is any incompatibility, please leave a message below. It is best to write down the browser version, so that I can test and fix the code.
Let’s look at the code below.

Copy code The code is as follows:

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 more conservative. If the value is obtained in the document, it will be returned directly. If not, the author often uses embeds Compatible with substitutes,
Of course IE needs special processing, but the author here often uses getElementById, while other places use window more often.
Of course getElementById is correct, it takes the ID of the object, because embeds only have names but no IDs.
Copy code The code is as follows:

var flash = document["myFlash"] || window ["myFlash"];

This method is relatively concise and clear, so I won’t be too verbose.
Copy code The code is as follows:

var flash = document["myFlash"];

I wrote this myself after reading various versions. Because IE6-10 under various browser virtual machines on my computer all passed the test, I also felt puzzled.
Of course, if it is officially used, I will still choose option one, but sometimes in special circumstances, if it does not need to be compatible with multiple browsers, you can consider this.

The following are actual examples. After obtaining the flash object using three methods, the flash is controlled and tested.
If anyone finds that the test cannot pass, please leave the browser version number and the solution that cannot pass. Thanks in advance.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn