简体   繁体   English

java applet:如何从applet调用/访问网页的javascript变量?

[英]java applet: How to invoke/access javascript variable of web page from applet?

An example: 一个例子:

getAppletContext().showDocument(
  new URL("javascript:alert(document.getElementById('textbox').value);"
));

Instead of alerting this textbox value, I want to use this value further as string variable in my applet program. 除了要提醒此文本框值之外,我还想在applet程序中将此值进一步用作字符串变量。 How can i achieve this? 我怎样才能做到这一点?

Multiple ideas for different approaches. 针对不同方法的多种想法。 Here are some: 这里有一些:


Put the input-field in the Applet. 将输入字段放在小程序中。 Instead of adding an <input> -tag to the HTML-page, you can get input in the Applet by adding AWT components (or, when using a JApplet you can use the newer Swing toolkit) to it. 您可以通过向其添加AWT组件(或者,当使用JApplet时可以使用较新的Swing工具箱)在Applet中获取输入,而不是在HTML页面中添加<input> -tag。 Although Swing should be preferred. 尽管应该首选Swing。

Of course, if you can't change the applet-code, this is not an option. 当然,如果您不能更改小程序代码,则不是一种选择。


Use the <param> -tag. 使用<param>标记。 You can give your applet some initial values (eg parameters), by using the <param> -tag in the <applet> -tag as shown by this example from the tag-doc : 您可以通过在<applet> -tag中使用<param> -tag来为applet提供一些初始值(例如参数),如以下示例所示,来自tag-doc

<applet code="A21" width="256" height="256" archive="toir.jar">
  <param name="img" value="test.gif">
</applet>

However, as I said, these are initial values. 但是,正如我所说,这些是初始值。 If you change them while the applet is already running, they won't change in the Applet itself. 如果您在小程序已经运行时对其进行了更改,则小程序本身将不会更改。

So, if the values in your <input> -tag are for initial values, you can use this (and manipulate the value -attribute with JS). 因此,如果<input> -tag中的值用于初始值,则可以使用此value (并使用JS操作value -attribute)。 If you want to change the values while the applet is running, this only works if you restart the applet (which might not be the best idea). 如果要在运行小程序时更改值,则仅在重新启动小程序时才有效(这可能不是最好的主意)。


Use "Java-to-Javascript Communication". 使用“ Java到Java的通信”。 There is a certain document introducing the JSObject -class. 有一个介绍JSObject类的文档。 It can be found here . 可以在这里找到。 There is also the "Common DOM API" which can be used to access the DOM-structure of the HTML-page. 还有“公共DOM API”,可用于访问HTML页面的DOM结构。 It sounds like what you're searching for, but it seems a little outdated. 听起来像您在寻找什么,但似乎有些过时了。

Als, this seems to be no standard (just like the <applet> -tag) and therefor, some browsers might not or not completely implement support for that. 不错,这似乎不是标准的(就像<applet> -tag一样),因此,某些浏览器可能没有或没有完全实现对此的支持。 You'll have to try it out. 您必须尝试一下。

// provide a JS function to return the value of textbox
// call it to get the value..
String userInput = JOptionPane.showInputDialog(this, value, ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM