如何在java applet里面调用网页里面的javascript对象?(300分)

  • 主题发起人 主题发起人 cAkk
  • 开始时间 开始时间
C

cAkk

Unregistered / Unconfirmed
GUEST, unregistred user!
其实我是想在applet里面使用cookie,如果有别的解决办法也可.
要求:用VJ++实现,因为我只有这个IDE.
 
<p>Applet调用Javascript,下面代码: <br>
<br>
[Java Applet] <br>
import java.applet.*;
<br>
import java.net.*;
<br>
<br>
public class theApplet extends Applet <br>
{ <br>
public void init() <br>
{ <br>
String sAbc = &amp;quot;I am going to HTML Tag thru the JavaScript's Function&amp;quot;;
<br>
try <br>
{ <br>
getAppletContext().showDocument (new URL(&amp;quot;javascript:doAlert(/&amp;quot;&amp;quot;
+sAbc+
&amp;quot;/&amp;quot;)&amp;quot;));
<br>
getAppletContext().showDocument(new
URL(&amp;quot;javascript:jumpFunction(/&amp;quot;#JUMP/&amp;quot;)&amp;quot;));
<br>
} <br>
catch (MalformedURLException me) { } <br>
<br>
} <br>
} <br>
<br>
[JavaScript and HTML] <br>
&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;/HEAD&amp;gt;
<br>
&amp;lt;BODY&amp;gt;
<br>
&amp;lt;SCRIPT language=&amp;quot;JavaScript1.2&amp;quot;&amp;gt;
<br>
functiondo
Alert(sXyz) <br>
{ <br>
alert(sXyz);
<br>
} <br>
function jumpTo(jumpingTo) <br>
{ <br>
self.location=jumpingTo;
<br>
} <br>
&amp;lt;/SCRIPT&amp;gt;
<br>
&amp;lt;APPLET CODE=&amp;quot;theApplet.class&amp;quot;
NAME=&amp;quot;theApplet&amp;quot;
MAYSCRIPT
HEIGHT=400 WIDTH=100&amp;gt;
<br>
&amp;lt;/APPLET&amp;gt;
<br>
&amp;lt;P&amp;gt;&amp;lt;BR&amp;gt;&amp;lt;BR&amp;gt;
<br>
&amp;lt;BR&amp;gt;Just writing some text as the&amp;lt;BR&amp;gt;Browser has to jumpdo
wn to &amp;lt;BR&amp;gt;the
line where we mentioned&amp;lt;BR&amp;gt;to jump to&amp;lt;BR&amp;gt;&amp;lt;BR&amp;gt;&amp;lt;BR&amp;gt;&amp;lt;BR&amp;gt;
<br>
&amp;lt;/P&amp;gt;
<br>
&amp;lt;P&amp;gt;
<br>
&amp;lt;BR&amp;gt;
<br>
&amp;lt;A NAME=&amp;quot;JUMP&amp;quot;&amp;gt;The result of the output of applet the browser should jump
to this place&amp;lt;/A&amp;gt;
<br>
&amp;lt;/BODY&amp;gt;
<br>
&amp;lt;/HTML&amp;gt;
<br>
如果客户端是IE的话,需要import netscape.javascript.*;
<br>
并且在applet将window换成JSObject. <br>
</p>
 
其实Javascript调用Applet更容易一些,
如果你需要的话,也可以贴一些代码上来。
 
如果使用JSObject的话,可以参考以下方法
JSObject win = JSObject.getWindow(this);
JSObjectdo
c = (JSObject) win.getMember("document");
JSObject loc = (JSObject)do
c.getMember("location");
 
看上去不错,能给段代码实现cookie的操作吗?
抱歉我对java很外行的.
 
<p>前面说得不清楚,下面重新来一遍。<br>
<font color="#FF8080">One:</font>如果你的JDK是1.2.2以上(应该没问题吧)以上,请找到c:/jdk/jre/lib/jaws.jar<br>
将其添加入classpath.因为其中含有packge:netscape.javascript.Jsobject  <br>
<font color="#FF8080">VJ我没用过,我想它加入packge的功能还是应该有。:</font><br>
<font color="#0080FF">Two:</font>写你的HTML文件,用Javascript写两个方法分别写cookie和读cookie,下面附带一个例子<br>
&amp;lt;html&amp;gt;<br>
<br>
&amp;lt;head&amp;gt;<br>
&amp;lt;title&amp;gt;Testing&amp;lt;/title&amp;gt;<br>
&amp;lt;script&amp;gt;<br>
<br>
function setCookie(txt){<br>
document.cookie = txt;<br>
alert (&amp;quot;Setting cookie : &amp;quot;
+ txt);<br>
}<br>
<br>
function getCookie(){<br>
var cookieAfter =do
cument.cookie;<br>
alert (&amp;quot;Getting the cookie : &amp;quot;
+ cookieAfter);<br>
return cookieAfter;<br>
}<br>
<br>
&amp;lt;/script&amp;gt;<br>
&amp;lt;/head&amp;gt;<br>
<br>
&amp;lt;!-- The MAYSCRIPT attribute is required in order for the Java methods to be available
to the JavaScript functions. --&amp;gt;<br>
&amp;lt;applet code=&amp;quot;cakk.class&amp;quot;
Width=400 Height=50 MAYSCRIPT&amp;gt;<br>
&amp;lt;/applet&amp;gt;<br>
<br>
&amp;lt;/html&amp;gt;<br>
<br>
<font color="#80FF00">Three:</font>编写你的Applet,示例如下:<br>
//Title: JavatoJs<br>
//Version: 1.0<br>
//Copyright: Copyright (c) eguy<br>
//Author: eguy<br>
//Company:<br>
//Description:<br>
import java.awt.*;<br>
import java.awt.event.*;<br>
import java.applet.*;<br>
import netscape.javascript.JSObject;
<br>
<br>
public class cakk extends Applet {<br>
JSObject win;<br>
boolean isStandalone = false;<br>
TextField setck = new TextField();<br>
TextField getck = new TextField();<br>
Button button1 = new Button();<br>
Button button2 = new Button();<br>
GridLayout gridLayout1 = new GridLayout();<br>
<br>
//Get a parameter value<br>
public String getParameter(String key, String def) {<br>
return isStandalone ? System.getProperty(key, def) :<br>
(getParameter(key) != null ? getParameter(key) : def);<br>
}<br>
<br>
//Construct the applet<br>
public cakk() {<br>
}<br>
<br>
//Initialize the applet<br>
public void init() {<br>
try {<br>
jbInit();<br>
}<br>
catch(Exception e) {<br>
e.printStackTrace();<br>
}<br>
}<br>
<br>
//Component initialization<br>
private void jbInit() throws Exception {<br>
try{<br>
win = JSObject.getWindow(this);<br>
}catch (Exception e){<br>
//do
n't throw exception information away, print it.<br>
e.printStackTrace();<br>
}<br>
<br>
&amp;nbsp;&amp;nbsp;&amp;nbsp;
setck.setText(&amp;quot;Set cookie&amp;quot;);<br>
this.setLayout(gridLayout1);<br>
getck.setText(&amp;quot;Get cookie&amp;quot;);<br>
button1.setLabel(&amp;quot;Set&amp;quot;);<br>
button1.addActionListener(new java.awt.event.ActionListener() {<br>
<br>
public void actionPerformed(ActionEvent e) {<br>
button1_actionPerformed(e);<br>
}<br>
});<br>
button2.setLabel(&amp;quot;Get&amp;quot;);<br>
button2.addActionListener(new java.awt.event.ActionListener() {<br>
<br>
public void actionPerformed(ActionEvent e) {<br>
button2_actionPerformed(e);<br>
}<br>
});<br>
this.add(setck, null);<br>
this.add(button1, null);<br>
this.add(getck, null);<br>
this.add(button2, null);<br>
}<br>
<br>
//Start the applet<br>
public void start() {<br>
}<br>
<br>
//Stop the applet<br>
public void stop() {<br>
}<br>
<br>
//Destroy the applet<br>
public void destroy() {<br>
}<br>
<br>
//Get Applet information<br>
public String getAppletInfo() {<br>
return &amp;quot;Applet Information&amp;quot;;<br>
}<br>
<br>
//Get parameter info<br>
public String[][] getParameterInfo() {<br>
return null;<br>
}<br>
<br>
void button1_actionPerformed(ActionEvent e) {<br>
String msg[];<br>
msg = new String[1];<br>
<br>
// Create a local copy of the cookie text<br>
msg[0] = setck.getText();<br>
<br>
// Call the JavaScript function to set the cookie <br>
win.call(&amp;quot;setCookie&amp;quot;, msg);<br>
<br>
}<br>
<br>
void button2_actionPerformed(ActionEvent e) {<br>
<br>
String txt = (String)win.call(&amp;quot;getCookie&amp;quot;,null);<br>
getck.setText(txt);<br>
}<br>
}<br>
<font color="#FF8080">Four:这里Cookie的写入和读出均较简单,仅是测试。复杂的根据自己要求写吧。</font></p>
 
cAkk:请继续或结束问题
 
step by step,好强啊,eguy
 
接受答案了.
 
后退
顶部