Call java method from javascript on HTML5 - java

I learned how to call a java method from within a javascript code on the page when the applet is loaded with the applet tag that is something like this:
<applet name="myapplet" code="MyJavaApplet.class" width="480" height="432">
</applet>
<script>
(function() {
document.myapplet.myMethod();
})();
</script>
That works!
However applet tag is deprecated and the following does loads the applet but doesn't call the method:
<object name="myapplet" type="application/x-java-applet" width="480" height="432">
<param name="code" value="MyJavaApplet.class" />
</object>
<script>
(function() {
document.myapplet.myMethod();
})();
</script>
The code I have is not exactly that but is very similar.
I tried it on Opera and Chrome.
Is this a wrong implementation of the object class or I'm just not doing it right.
I don't have experience with javascript so I'm really in doubt here.
EDIT: Thanks for the quick replies.
It was a typo somewhere, I checked that document.myapplet was returning correctly through the console on dragonfly.
It worked when I wrote the page again.
I put an example here which prints on System.out so you'll need to open the Java console to see it working through both applet and object.

Related

Why browsers crop java applet?

I created a Java applet supposed to be used in an applet as applets should.
For some reason, all browsers (chrome, firefox, IE) cut it.
I tried using the repaint() and invalidate() methods. I didn't help...
But, interestingly, when I scroll the browser up or down (even a tiny scroll), it immediately fixes. Also, when I use showOptionDialog(Component, String) method, if fixes after the user clicks the OK button on the dialog. It fixes only when the component inputted is the applet itself.
I don't understand why it crops, or how to fix it.
The HTML calling the applet it is very simple. Here is an example of a short piece of HTML code which causes the problem:
<html>
<head>
<title>nispahit</title>
</head>
<body>
<center>
<APPLET CODE="Applet/AppletMain.class" archive="nnis.jar, log4j-1.2.16.jar" WIDTH=800 HEIGHT=600>
<param name="userName" value="http://www.nispahit.com/images/shalom.jpg">
<param name="ColorsContrast" value="241026555"/>
<param name="param2" value="14">
<param name="param3" value="2">
<param name="param3" value="2">
</APPLET>
</center>
</body>
</html>
A solution or workaround will help very much. Any insight would also be helpful.
Thanks.
The cropping happens outside of the applet. There is some block element around the applet (usually a <div>) which is too small to contain the whole applet.
Try to add a div with a red background around the applet so you can see quickly which part is rendered by the browser and which comes from the applet.

get parameter for JApplet not working

I am experiencing a little problem reading parameters from an html page to an applet.
my code (necesssary for question):
on html page:
<PARAM NAME = "name" VALUE = "Nicholus">
in applet (init):
String strName = getParameter("name");
The applet just decides to look at me instead of getting the name value..
a few google search shows im not the only one, except I haddn't yet found the solution, so I decided to post it here in case it was already resolved.
Compare your work with the following working code or post your code if problem still not solved.
Here is the ParamDemo.java code
import java.awt.*;
import java.applet.*;
public class ParamDemo extends Applet{
String strName;
public void start()
{
strName=getParameter("name");
if(strName==null)
strName="Not Found";
}
public void paint(Graphics g)
{
g.drawString("Name :"+strName,10,20);
}
}
Then the Applet.html
<html>
<body>
<applet code="ParamDemo" width="300" height="300">
<param name='name' value="Nicholas">
</applet>
</body>
</html>
Snapshot
[Ps:You need to compile your .java before using in HTML page and make sure that there is no problem related to leter-case of parameter-names as java is case-sensitive.Also the .class and .html files must be in same root folder).
I got it solved, my negligence... my html had many applet codes, the params were not printed within the applet declaration of the one that needs the params. thanks for contributing.

Putting my Java Slick game onto a website

I have made a GUI in Java using State Based Game, as it extends StateBasedGame and not JApplet its not a true applet, I want to put it on a website and am unsure on how to do this, I have been told that the following code allows me to make this into an applet using html and not having to edit the Java code:
<applet code="org.lwjgl.util.applet.AppletLoader"
archive="org.lwjgl.util.applet.AppletLoader"
codebase="."
width="640" height="480">
<param name="al_title" value="Ham Blaster">
<param name="al_main" value="org.newdawn.slick.AppletGameContainer">
<param name="game" value="org.javagame.Game">
<param name="al_jars" value="racegame.jar, lwjgl.jar, slick.jar">
<param name="al_windows" value="windows_natives.jar">
<param name="al_linux" value="linux_natives.jar">
<param name="al_mac" value="macosx_natives.jar">
<param name="separate_jvm" value="true">
</applet>
When I run the html code I get the following error on the centre of my screen:
An error occurred while loading the applet. Please contact support to resolve this issue. missing required applet parameter: al_main.
I have looked inside slick for the ApplerGameContainer but I can only find chrome files, I tried putting these in the same directory as the html file but I still get the same error.
If something seems very simple and may seem too obvious to ask me, please tell me because I am new to Java, my previous error with this code was because I did not have my html and lwjgl_util_applet.jar in the same directory, so please tell me anything that may help me.
Emm...
I am not pretty sure, but according to sources the error comes from init method
/*
* #see java.applet.Applet#init()
*/
public void init() {
state = STATE_INIT;
// sanity check
String[] requiredArgs = {"al_main", "al_logo", "al_progressbar", "al_jars"};
for(int i=0; i<requiredArgs.length; i++) {
if(getParameter(requiredArgs[i]) == null) {
fatalErrorOccured("missing required applet parameter: " + requiredArgs[i], null);
return;
}
//... }
}
so it seems your al_main param value is null or something :S
The problem is maybe someplace here
<param name="al_main" value="org.newdawn.slick.AppletGameContainer">
... maybe because I don't see you to init all the rest required params as "al_logo", "al_progressbar"
Anyways, it would be more helpful you to show the problem code
Report if it helps

Javascript does not find Applet in IE9

i have got a Problem with Javascript and it´s Communication to an applet.
In every Browser Javascript does find the applet and i can call it´s methods, but in IE9 it does not find the object/applet. heres the code
<div class="speichern" align="center">
<object type="application/x-java-applet" width="10" height="10" id="jsap" name="jsap">
<param name="archive" value="ABD_Downloadmanager.jar,ojdbc6.jar"> </param>
<param name="code" value="Speichern_Applet.class"> </param>
<param name="mayscript" value="yes">
<param name="scriptable" value="true">
</object>
</div>
and the Javascript call :
var appletObj = document.getElementsByName('jsap')[0];
var path = appletObj.test();
the test Method does just return a String return "C:/";
when i use the an alert(appletObj );
Firefox does show me object HtmlObjectElement
But the IE does only show object
i have tried this ways to get the applet without any success:
var appletObj = document.getElementsByID('jsap')[0];
var appletObj = document.getElementsByID('jsap');
var appletObj = document.jsap;
var appletObj = document.applets[0];
for me it looks like the IE simply cannot work with the object tag or something like this? someone got a solution for this or an idea why the IE can not find the object/applet?
Thanks for reading so far
I Just found out the Problem which came up in here.
I was using the style='visible:hidden' attribute. But a hidden DOM object, in this case the Object tag/ applet won´t be loaded by the IE. I just had to set the size to (0,0). Genius...

calling id from applet object throws js error in IE

When calling an applet id from JS giving Object doesn't support this property or method error.
Please check the code...
<!--[if !IE]> Firefox and others will use outer object -->
<object
classid = "java:com.mypack.myclass.ABCClass.class"
type = "application/x-java-applet"
height = "200"
width = "480"
id = "myappletId">
<!-- Konqueror browser needs the following param -->
<param name="cache_archive" value="a.jar, b.jar, c.jar" />
<param name="codebase" value="applets/" />
<param name="OnMouseLeftClickJS" value="showModal()">
<!--<![endif]-->
<!-- MSIE (Microsoft Internet Explorer) will use inner object -->
<object
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab"
height = "200"
width = "480"
id = "myappletId">
<param name="code" value="com.mypack.myclass.ABCClass" />
<param name="cache_archive" value="a.jar, b.jar, c.jar" />
<param name="OnMouseLeftClickJS" value="showModal()">
<param name="codebase" value="applets/" />
<strong>No Java Support</strong>
</object>
<!--[if !IE]> close outer object -->
</object>
<!--<![endif]-->
Now, in my showModal() js function I have,
var myId = document.getElementById("myappletId"); //At this point I am getting the object data.
I perform some operations in my modal window which is a jquery modal window and I have a button in it. When I click on that I am calling a function say xyz().
function xyz() {
var myId = document.getElementById("myappletId");
//At this point myId is getting empty.
}
This is happening in only IE Browser. I tested in IE8.
Works good in Firefox and chrome.
I am not navigating my page and on view source I can see my object tag.
Please help me out.
Thanks in advance...
In your comments you have
<!-- MSIE (Microsoft Internet Explorer) will use inner object -->
How about using document.getElementById('myappletId').getElementById('innerApplet') ?
It would be great idea to use unique id for inner applet, maybe then your solution would work.
Thanks for your effort guys, the mistake is in my code. When I open a modal window making the object tag visibility hidden in the back. Even I am make it visible after closing the modal window, This IE is not identifying it. Weird but true...

Categories