Home > Article > Web Front-end > Flex example code to obtain client IP and computer name through JS_javascript skills
First of all, let me explain that using JS to obtain data does not mean calling webservices and httpservices.
In each of our FLex web projects, there is a folder bin-debug, which contains an index.html file. My personal understanding is this. First, when the web page is run, the mxml file will be compiled. For swf, how to display swf in the browser is through this html file, which is equivalent to embedding a swf object component in the html file. www.jb51.net
Therefore, I personally think it is feasible to write js code in this html to obtain the IP address. Hence the following code.
Add script tag in index.html, the actual code is as follows:
How to call it in flex, then use this class of Flex: flash.external.ExternalInterface.
For the ExternalInterface class, Adobe explains it this way:
The ExternalInterface class is an external API, an application programming interface that enables direct communication between ActionScript and the Flash Player container, for example, an HTML page containing JavaScript. Adobe recommends using ExternalInterface for all communication between JavaScript and ActionScript.
In Flash Player, you can use JavaScript in an HTML page to call ActionScript functions. ActionScript functions can return a value that JavaScript immediately receives as the return value of the call.
This function replaces the fscommand() method.
The ExternalInterface class is available in the following browser and operating system combinations:
Browser Operating System Operating System
Internet Explorer 5.0 and above Windows
Netscape 8.0 and above Windows MacOS
Mozilla 1.7.5 and above Windows MacOS
Firefox 1.0 and Later Windows MacOS
Safari 1.3 and later MacOS
Flash Player 9.0.31.0 and later for Linux The ExternalInterface class is supported in the following browsers:
Browser
Mozilla 1.7.x and higher
Firefox 1.5.0.7 and higher
SeaMonkey 1.0.5 and higher
ExternalInterface class requires the user's web browser to support ActiveX® or the NPRuntime API exposed by some browsers for plug-in scripting. Even browser and operating system combinations not listed above should support the ExternalInterface class if they support the NPRuntime API.
Note: When embedding a SWF file into an HTML page, make sure to set the id attribute and that the id and name attributes of the object and embed tags do not contain the following characters:
. - * / /
Note: Flash Player version 9.0.115.0 and later allows the . (period) character in the id and name attributes.
Programmatically opening a pop-up window using this class may not succeed in Flash Player 10 and later running in a browser. Different browsers (and browser configurations) may block pop-ups at any time; there is no guarantee that any pop-ups will be displayed. However, to be as successful as possible, use this type of open popup window only in code that executes as a direct result of user action (for example, in an event handler for a mouse click or keypress event).
Using ActionScript, you can perform the following actions on HTML pages:
Call any JavaScript function.
Pass any number of arguments with any names.
Pass various data types (Boolean, Number, String, etc.).
Receives the return value from a JavaScript function.
By using JavaScript on HTML pages, you can:
Call an ActionScript function.
Pass parameters using standard function call notation.
Return a value to a JavaScript function.
Flash Player currently does not support SWF files embedded in HTML forms.
Note: In Adobe AIR, the ExternalInterface class can be used to communicate between the JavaScript of an HTML page loaded in an HTMLLoader control and the ActionScript embedded in the SWF content of that HTML page.
Use this class in our mxml file. Call the function to obtain ip and pcname in js:
pcName = ExternalInterface.call("getClientPcName");
ip = ExternalInterface.call("getClientIp");
You can try Alert and see if it works. There is another problem. If you add your machine as a server, the IP obtained on the server will be: 127.0.0.1. I believe you will have a way to deal with it.