Background Introduction :
Recently engaged in a project, involving the selection of pictures, cropping, uploading, etc., due to browser security issues, JS can not get to <input type= "file" > The path of the selected files, and the photo clipping, cutout, etc. by the HTML5 implementation of the functions of the major browser compatibility, I'm not too bold. This time the introduction of Flash, and then all JS do not make flash to do, and then by JS to control the page elements. So began the JS and as interactive tour, listen to do flash Uncle said, Flash JS function is very good, and JS tune as is not easy. The final situation is as the JS error, spent a lot of time on the above, of course, the beginning of JS tune as also took a detour, because there was no previous dealings with Flash, so can only be said by others, but it seems not our JS problem, because the online ready-made method on a lot of, nonsense not much to say, Share the code in the project with JS calling as.
Important content
Get Flash Object
function getswf (name) {
var E=document.getelementbyid (name);
Return (Navigator.appName.indexOf ("Microsoft")! =-1)? E:e.getelementsbytagname ("embed") [0];
}
Why do this, because embedding flash in a browser is generally in the following format:
<object>
<embed></embed>
</object>
Because of IE (Microsoft Home), other (Mozilla's Firefox, Google Chrome, etc.) in the HTML document parsing, there are differences, so if the face of different browsers, universal a method, I do not know where the wrong, but you are really wrong!
Code explanation
The above function, getswf (), is to get the embedded Flash object in the document, Navigator is the browser object, and theNavigator object contains information about the browser.
The relevant information can be consulted: http://www.w3school.com.cn/jsref/dom_obj_navigator.asp;
So the following appName is the Navigator object called "AppName" attribute, this property records the Navigator.appname gets the name of the browser, the current 5 major mainstream browser appName values are as follows:
IE: Browser name: Microsoft Internet
Ff,chrome,opera,safari: Browser name: Netscape
Browser appname test Address: Http://www.w3school.com.cn/tiy/t.asp?f=hdom_browser;
IndexOf () is a JavaScript function, and theindexOf () method returns the position of the first occurrence of a specified string value in a string .
Stringobject.indexof ("str", num); Stringobject is the string object being searched for, STR is the string to find, num is the starting position, and if the "str" string is queried to exist in Stringobject, The first occurrence is returned, and if the string value to be retrieved does not appear, the method returns-1;
var e = document.getElementById (name); Is the node object that gets the value of the property ID in the document, and assigns the object to E,
Return (Navigator.appName.indexOf ("Microsoft")! =-1)? E:e.getelementsbytagname ("embed") [0]; it looks like it's complicated. Here is a tri-wood operation (all should know about programming), the specific format is if (a)? B:c, meaning that when if (a) is true, the value is B,if (a) is not true, the value is C. This long code can be decomposed into an if (a)? B:C; format
Navigator.appName.indexOf ("Microsoft")!=-1 e e.getelementsbytagname ("embed") [0] these three parts
if (Navigator.appName.indexOf ("Microsoft")!=-1) {alert ("My browser is not the IE kernel"); }else{"My browser is IE kernel"}
When the pop-up window is "My browser is not the IE kernel", it indicates that my browser name does not include "Microsoft", that is, the use of IE browser, this time to get the Flash object is the document <object></object> object, in the case of IE, gets the <embed></embed> object, regardless of whether the object is obtained, eventually return to the calling function with return, so that the corresponding Flash object can be obtained in different browsers.
Overall Solution :
After getting the Flash object, you can call the flash inside the method, or flash inside the properties.
var objname = getswf ("Flashtojs");
Flash_selfiles method that invokes an object
if (objname) {
Objname.flash_cutpic (ARG);
}else{
Console.log ("Not acquired to Object");
}
This is transferred to the flash inside the Flash_selfiles () method.
Last Words of speech
In the page or try to minimize the use of flash, especially to flash too much reliance on the AS and JS interaction is still hidden a lot of traps. The report is complete!