简体   繁体   English

Applet JavaScript通信

[英]Applet JavaScript Communication

On my web page I am running an applet and in order to warn user for certain inputs to the applet, I am using printing the errors to the html page. 在我的网页上,我正在运行一个applet,并且为了警告用户对applet的某些输入,我正在使用将错误打印到html页面上。 In order to do this I am using javascript applet communication. 为了做到这一点,我正在使用javascript applet通信。 Below is the code for the Javascript: 以下是Javascript的代码:

<script type="text/javascript">
var counter=0;

function RemoveAppletErrorMessage()
{
    var AppletErrorTag=document.getElementById("AppletErrorMessages");
    if(AppletErrorTag!=null)
    {
        document.body.removeChild(AppletErrorTag);
    }
}

function updateWebPage(s)
{
    if(counter>=1) RemoveAppletErrorMessage();
    var parag=document.createElement('P');
    parag.setAttribute("id","AppletErrorMessages");
    var txt=document.createTextNode(s);
    parag.appendChild(txt);
    document.body.appendChild(parag);
    counter++;
}
</script>

Here, the Javascript function updateWebPage(s) function is called from the Java applet when a JButton is clicked with the following code: 在这里,当使用以下代码单击JButton时,将从Java小程序中调用Javascript函数updateWebPage(s)函数:

if(jso != null && !errorMessage.isEmpty())
try {
    jso.call("updateWebPage", new String[] {errorMessage});
    return;
}
catch (Exception ex) { ex.printStackTrace(); }

In terms of communication everything works fine. 在交流方面,一切正常。 However, when the JButton is clicked the second time, I am trying to clear up the error messages with RemoveAppletErrorMessage() calling from updateWebPage(s). 但是,当第二次单击JButton时,我正在尝试使用从updateWebPage调用的RemoveAppletErrorMessage()清除错误消息。 It seems like every time I hit the JButton the error messages are appended to the web page which I do not want. 好像每次我按下JButton时,错误消息都会附加到我不想要的网页上。 I have tried to clear it with .innerHTML='' which did not work in the first place, so I changed my strategy to adding a node and checking if the node exists, clearing that node and adding again. 我试图用.innerHTML =''清除它最初没有用,所以我将策略更改为添加一个节点并检查该节点是否存在,然后清除该节点并再次添加。 What might be going on wrong? 可能出什么问题了?

I'd open a div with an id of AppletErrorMessages and use jQuery in my updateWebPage() function like this (eg stop creating a new paragraph every time). 我将打开一个ID为AppletErrorMessages的div,并在像这样的updateWebPage()函数中使用jQuery(例如,每次都停止创建新段落)。

function updateWebPage(s)
{
  $("#AppletErrorMessages").html(s);
}

可能是您在Java中附加了errorMessage变量?

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

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