google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Thực thi JavaScript từ ứng dụng Java Applet Mặc dù có tên khá giống nhau, Java và JavaScript lại là hai ngôn ngữ lập trình hoàn toàn khác nhau nhưng đều có thể tạo nên các ứng dụng trên nền web. JavaScript được hỗ trở bởi tất cả các trình duyệt nhưng Java thì không. Tuy là hai ngôn ngữ hoàn toàn khác nhau nhưng các ứng dụng của chúng có thể giao tiếp với nhau, bài viết hướng dẫn sử dụng JavaScript này sẽ giúp bạn biết cách xây dựng cầu nối để ứng dụng của hai ngôn ngữ lập trình web này giao tiếp với nhau, vui lòng vào trang chi tiết để xem thêm.


Nhãn: thực thi JavaScript, ứng dụng Java, Java Applet, ứng dụng nền web, giao tiếp

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

While creating the UTM Coordinate Converter applet, I was looking for examples on how to get a Java Applet, to call a Javascript function in the html. I found various bits of information and some examples (that didn't always work), and so decided I would pool the information and try to give a clear tutorial on how to do this. This tutorial assumes Java6 but should work for Java5 as well. I'm not too sure about earlier version of Java.

Step 1: Get plugin.jar

plugin.jar contains the classes needed for an applet to call javascript functions. It is necessary to have plugin.jar in the classpath of your development environment. Plugin.jar can be found in /lib directory of your java installation (example on Windows: C:\Program Files\Java\jre6\lib). It is not necessary to specify plugin.jar in the classpath when putting your applet on your webpage, since the Java plugin will already have plugin.jar (see example in Step 5)

Step 2: import required classes

In the Applet Code add the following import:

Code:

import netscape.javascript.JSObject;

Don't worry about 'netscape' in the package name, it works for IE just fine.

Step 3: create your javascript function for your webpage:

Code:

<script type="text/javascript">
  //Javascript function
  function testFunction(param1, param2) {
    alert('Called by applet: ' + param1 + param2);
  }
</script>

In this example, the javascript just displays an alert box, but it could do anything you want. This function also accepts parameters, to show how parameters can be passed in. Note: the javascript can either be included directly in the HTML from a separate .js file.

Step 4: Add a call to the javascript in the applet java code

Code:

private void callJavascriptFunction(){
  try {
    JSObject window = JSObject.getWindow(this);
    String param1 = "Hello from ";
    String param2 = "a Java applet";
    Object[] params = {param1, param2};
    window.call("testFunction", params);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

This Java function creates a JSObject object called window. It then sets up a couple of parameters (as Strings) and puts them into an Object array. It finally calls thejavascript function created earlier using the call() method and passes in the name of the javascript function, and the params array.

Step 5: Add applet to html page

Below is the way I add applets to HTML pages, which seems to work across all browsers. The key for the javascript to work is to include the param name="MAYSCRIPT value="true"

Code:

<!--[if !IE]>-->
<object classid="java:CoordinateConverterApplet.class"
    type="application/x-java-applet"
    codebase="../assets/applets"
    archive="CoordinateConverter.jar"
    width="450" height="760" >
    <!-- Konqueror browser needs the following param -->
    <param name="archive" value="CoordinateConverter.jar" />
    <param name="MAYSCRIPT" value="true" />
    <p>Coordinate Converter Applet</p>
<!--<![endif]-->
  <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
      width="450" height="760" >
    <param name="code" value="CoordinateConverterApplet" />
    <param name="codebase" value="../assets/applets/" />
    <param name="archive" value="CoordinateConverter.jar" />
    <param name="MAYSCRIPT" value="true" />
    <p>Coordinate Converter Applet</p>
  </object>
  <!--[if !IE]>-->
</object>
<!--<![endif]-->

Step 6: Try it out!

In your Java Applet code, when you make a call to callJavaScriptFunction(), it will call the Javascript function, and if everything works correctly, it will display an alert box as follows:

Java Script Alert

To see a live working example of calling javascript from an applet, you can go to the UTM Coordinate Converter tool. In this tool, when clicking on a Convert button, the applet calls a Javascript function, passing in the coordinate, and the javascript updates the Google Map.

Link to JSObject API documentation: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/LiveConnect/JSObject#getWindow

iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web