About JS to get flash objects, there are a lot of examples on the Internet, I also tried a lot of methods.
Although it can be used, but not what I want most,
Later looked at Baidu, although very standard, all kinds of circumstances have taken into account, but the amount of code is not not optimistic,
Back and forth nearly 20 lines of code, after compression also has 864 bytes.
So this article was born.
I want to test and share some of the more satisfying ways I've collected online.
Also by the way please help test the compatibility, there are incompatible in the following leave a message, it is best to write the browser version, so I also good test and repair the code.
Let's look at the code here.
Copy Code code 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 relatively conservative, if the document is taken to the value of the direct return, if not taken, the author often used embeds compatible substitutes to take,
Of course, IE is to special treatment, but here the author often getElementById, and other places with window more.
Of course getElementById is not wrong, he takes the ID of object, because embeds only name has no ID.
Copy Code code as follows:
var flash = document["Myflash"] | | window["Myflash"];
This method is simpler and clearer, and I don't have much to nag about.
Copy Code code as follows:
var flash = document["Myflash"];
This is I read a variety of versions, I wrote, because I have a variety of browsers on the computer + virtual machine under the ie6-10 are tested, I also feel puzzled.
Of course, if the official use, I will still choose scheme one, but sometimes special circumstances, do not need to be compatible with multiple browsers, you can consider this OH.
The following is a practical example, using three methods to obtain the Flash object, and Flash control test.
If a friend found that can not test pass, trouble left the browser version number and can not pass the scheme, the younger brother first thanked.