javascript 內嵌 java code

今天想要找出如何用 javascript 取得自己電腦上的 local ip address 上網求助 google 大神的結果,居然意外的發現,原來 javascript 並不只是名字相似,居然 javascript 可以內嵌 java code, 所以我可以使用 java.net.getLocalhost().getHostAddress() 來取得目前所在電腦上的 host address.

參考資料:
1.getting the user’s IP address
2.determining the user’s screen size

網頁上所貼的 code 並且我加上一些 tag 使其成為一個完整的網頁。

<html>
<head>
<title>Get User IP address</title>
</head>
<body>
<script>
/* There are a few instances in which the browser cannot ascertain the user's address, so we will instruct the browser to ignore errors by setting the onerror handler to null: */
window . onerror = null;

/* We will also give hostaddress and hostname a default value in case the address look-up fails: */
hostaddress = hostname = "(unknown)";

/* Now we will try to gather the host information: */
localhost = java.net.InetAddress.getLocalHost();
hostaddress = localhost.getHostAddress();
hostname = localhost.getHostName();

/* The Java methods used above are capable of throwing exceptions. When Java exceptions occur within JavaScript, the script body is aborted. In order to make sure that the following statements get a chance to execute, we must include them in a separate script body: */

</script>

<script>
document . writeln ("<p>Your IP address is <b>" + hostaddress + "</b>.</p>");
document . writeln ("<p>Your hostname is <b>" + hostname + "</b>.</p>");

</script> <p>Any questions?</p>
</body>
</html>
廣告

One Response to javascript 內嵌 java code

  1. eddy 說道:

    doesn’t work in IE7

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

%d 位部落客按了讚: