Flex obtains the Client IP address and computer name through JS

Source: Internet
Author: User

I have written two posts to obtain IP addresses. However, we recently found a better way to obtain the Client Ip address and computer name. Share with you.

First, we will explain that using JS to obtain webservices and httpservices is not called.

In each of our FLex web projects, there is an index.html file in the bin-debug folder. In my personal understanding, when a webpage is running, the mxml file will be compiled into swf. How can swf be displayed in the browser, this html file is equivalent to embedding a swf object component in the html file. Www.2cto.com

Therefore, it is feasible to write JavaScript code in this html to obtain the IP address. The following code is displayed.

Add the script tag to index.html. The actual code is as follows:

<Script type = "text/javascript">

Function getClientPcName ()

{

// Alert ("Please set" enable "for" initialization and script running of ActiveX controls not marked as secure "in your browser's Internet Options "! \ N and then refresh the page to log on! ");

/// Window. onerror = killErrors; + "/" + WshShell. UserName;

Var WshShellPcName = new ActiveXObject ("WScript. Network ");

Var ComputerName = WshShellPcName. ComputerName;

// Add a trusted site (http: // 192.168.1.5)

// Alert (ComputerName );

Return ComputerName;

}

Function getClientIp ()

{

Var WshShellIp = new ActiveXObject ("rcbdyctl. Setting ");

Var ip = wshellip. GetIPAddress;

Return ip;


}

</Script>

In this way, the code equivalent to the background of the obtained ip address and pcname can be written.

How can we call it in flex? The Flex class is used: flash. external. ExternalInterface.

Adobe explains the ExternalInterface class as follows:

The ExternalInterface class is an external API, which is an application programming interface that implements direct communication between the ActionScript and Flash Player containers, for example, HTML pages containing JavaScript. Adobe recommends that you use ExternalInterface to implement all communication between JavaScript and ActionScript.

In Flash Player, you can use JavaScript on the HTML page To Call The ActionScript function. The ActionScript function can return a value, and JavaScript immediately receives it as the return value of the call.

This function replaces the fscommand () method.

The ExternalInterface class can be used in the combination of the following browsers and operating systems:

Browser Operating System
Internet Explorer 5.0 and later Windows
Netscape 8.0 and later Windows MacOS
Mozilla 1.7.5 and later Windows MacOS
Firefox 1.0 and later Windows MacOS
Safari 1.3 and later versions MacOS
Flash Player 9.0.31.0 and later for Linux support the ExternalInterface class in the following browsers:

Browser
Mozilla 1.7.x and later
Firefox 1.5.0.7 and later
SeaMonkey 1.0.5 and later
ExternalInterface requires that your Web browser support ActiveX®Or the NPRuntime API exposed by Some browsers to implement plug-in script processing. Even if the browser and operating system combinations are not listed above, if they support the NPRuntime API, they should also support the ExternalInterface class.
Note: When embedding a SWF file into an HTML page, make sure to set the id attribute. The id and name attributes of the object and embed labels do not contain the following characters:

.-+ */\
 
Note: Flash Player 9.0.115.0 and later versions allow the use of. (period) characters in the id and name attributes.

In Flash Player 10 and later versions running in the browser, opening the pop-up window in programming mode may not succeed. Different browsers (and browser configurations) may block the pop-up window at any time. There is no guarantee that any pop-up window can be displayed. However, to be as successful as possible, please use this class to open a pop-up window (for example, in the event processing function where you click or press an event) only in the code that is executed as the direct result of the user operation ).

You can perform the following operations on the HTML page by using ActionScript:

Call any JavaScript function.
Pass any number of parameters with any name.
Transmits various data types (Boolean, Number, String, and so on ).
Receives the return value from the JavaScript function.
By using JavaScript on the HTML page, you can:

Call the ActionScript function.
PASS Parameters Using standard function call notation.
Return the value to the JavaScript function.
Flash Player currently does not support embedding SWF files in HTML forms.

Note: In Adobe AIR, The ExternalInterface class can be used to implement communication between the JavaScript of the HTML page loaded in the HTMLLoader control and the ActionScript embedded in the SWF Content of the HTML page.

Use this class in our mxml file. Call the function to obtain the ip address and pcname in js:

PcName = ExternalInterface. call ("getClientPcName ");

Ip = ExternalInterface. call ("getClientIp ");


You can try Alert to see if it is correct. There is another problem. If you add your machine as the server, the ip address obtained on the server will be 127.0.0.1. I believe you will find a solution to solve this problem.

End. Good luck.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.