We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Javascript Embed Applet
Pages: 1 2 
Javascript Embed Applet (Read 9483 times)
Javascript Embed Applet
Dec 23rd, 2006, 12:49am
 
I've recently had to use deconcept's javascript embed method for Flash because of the grey box of death.

But since I've learned how to use javascript to vanish and reappear <div>s on my page as well as rewrite their properties, I realised that I could use this to dynamically add applets to a page.

This means that the html grabber in my flash portfolio would be able to see an applet on a page and if the user clicked a link they would find that below the flash content an applet would appear.

If anyone has any links that would help me build this .js file it would help me a lot (I'm not that experienced with javascript).

Regardless of assistance or not I intend to append this thread with a file for javascript generating applets.
Re: Javascript Embed Applet
Reply #1 - Dec 27th, 2006, 12:13am
 
I went to webdeveloper.com and was taught a couple of methods. That means we've got a function that works from http request.
Code:

<html>
<head>
<title>Untitled Document</title>
<style>
.one{
background-color:#FF0000;
width:400px;
height:400px;
float:left;
}
.two{
background-color:#00FF00;
width:400px;
height:400px;
float:left;
}
.three{
background-color:#0000FF;
width:400px;
height:400px;
float:left;
}
</style>
<script language="javascript">
function JavaApplet(div, code, archive, width, height, mayscript){
var d=document.getElementById(div);
var string = '<applet code="'+code+'" archive="'+archive+'" width="'+width+'" height="'+height+'" mayscript="'+mayscript+'">';
string += '</applet>';
d.innerHTML = string;
}
</script>
</head>
<body>
<div class="one"></div>
<div class="two" id="appletDiv"></div>
<div class="three"></div>
<a href='javascript:JavaApplet("appletDiv", "snakeAI5","http://www.robotacid.com/PBeta/snakeAI5/snakeAI5.jar","400","400","true");'>Add Applet</a>
</body>
</html>

This is about as simple as you can magic an applet on to a HTML that's already loaded. You could make a menu I guess without ever reloading the page, just the applets.

I need to develop this into an object so you can have params , a goddamned loading screen, jogl settings and alternate content.
Re: Javascript Embed Applet
Reply #2 - Dec 28th, 2006, 2:44pm
 
HTML
Code:

<html>
<head>
<style>
.one{
background-color:#FF0000;
width:400px;
height:400px;
float:left;
}
.two{
background-color:#00FF00;
width:400px;
height:400px;
float:left;
}
.three{
background-color:#0000FF;
width:400px;
height:400px;
float:left;
}
</style>
<script type="text/javascript" src="AppletObject.js"></script>
</head>
<body>
<div class="one"></div>
<div class="two" id="appletDiv"></div>
<div class="three"></div>
<a href='javascript:var ao = new AppletObject("snakeAI5","http://www.robotacid.com/PBeta/snakeAI5/snakeAI5.jar","400","400","true");ao.write("appletDiv");'>Add Applet</a>
</body>
</html>

JavaScript file AppletObject.js
Code:

// Java Applet Object
function AppletObject(code, archive, width, height, mayscript){
this.code = code;
this.archive = archive;
this.library = "";
this.width = width;
this.height = height;
this.mayscript = mayscript;
this.write = function(div){
var d=document.getElementById(div);
var string = '<applet code="'+this.code+'" archive="'+this.archive+this.library+'" width="'+this.width+'" height="'+this.height+'" mayscript="'+this.mayscript+'">';
for(var i = 0; i < this.param.length; i++){
string += '<param name="'+this.param[i].name+' value="'+this.param[i].value+'>';
}
string += this.alt;
string += '</applet>';
d.innerHTML = string;
};
this.addParam = function(name, value){
var appletParam = new Object();
appletParam.name = name;
appletParam.value = value;
this.param.push(appletParam);
};
this.addLibrary = function(file){
if(file.charAt(0) == ',' || this.library.charAt(this.library.length-1) == ','){
library += file;
} else {
library += ',' + file;
}
}
this.folder = this.archive.substring(0, this.archive.lastIndexOf("/")) + "/";
this.alt = 'To view this content, you need to install Java from <A HREF="http://java.com">java.com</A>';
this.param = new Array();
}

Voila. One AppletObject.js. (functions: addParam(name, value), addLibrary(file), write(div))

I've found you can pretty much concat a whole world of js commands into a href, so adding params and libraries could be managed by some behind the scenes functions.

For jogl applets you just need to change the code value and set a few params.

I need to build a html ripper for Flash now that is going to get the properties of applets it finds on pages on my site. I've got a basic one running inside the Flash, but I'd rather format everything in PHP to give to Flash, rather than let Flash make a hash of it with it's crappy string management.

Anyone wishing to pelt me with their 2 cents is welcome.
Re: Javascript Embed Applet
Reply #3 - Jan 4th, 2007, 9:24pm
 
Bad News for jogl

I was pretty certain I'd tested opengl with the JavaScript mount, but my last test didn't work (which is pretty annoying seeing as I've spent the last hour writing a script in Perl to scan a html with a jogl applet and mount it elsewhere).

Does anyone have a url to a signed applet? I'm trying to rule out possibilities at the moment.

Here's my online demo, with working and non-working applets

http://www.robotacid.com/misc/css_js_applet_object.html
Re: Javascript Embed Applet
Reply #4 - Jan 5th, 2007, 1:02am
 
interesting work, any ideas about fallbacks in case js is disabled?

about opengl, you forgot to close the quotes inside the params:
http://bezier.de/xhtml/opengl/js/appletobject.js
http://bezier.de/xhtml/opengl/index_js.html

F
Re: Javascript Embed Applet
Reply #5 - Jan 5th, 2007, 5:57pm
 
Thanks for that. God I'm dozy at times.

It's giving me errors on my end. It won't load stuff locally it's saying. Since you've proved the concept, it's definitely me.

I'm not bothered about lack of js, mostly because it's for this:

http://www.robotacid.com/portfolio.php

...and I'm testing the Java mounting here:

http://www.robotacid.com/portfolio.html

It uses SWFObject.js to mount the Flash, so js is mandatory.

It's because I loathed the idea of writing one content for a Flash portfolio and more content for my html site. So I cobbled someone's php html2xml script with my own xml digger to mine content from my html pages. I've written a better html2xml by formatting the text perfectly for Flash and wrapping it in CDATA tags (in Perl, and yes all my source can be downloaded). It even grabs img tags and creates an array to give to the Flash image carousel.

But what about all my java applets? If only I could magic them on to the html page...

Cue appletobject.js, lightning, evil laugh.
Re: Javascript Embed Applet
Reply #6 - Jan 5th, 2007, 8:44pm
 
you could try and set a codebase inside the applet tag. maybe that's gonna resolve your problems with the .jars not loading (i guess that's what's happening).

i was just asking about the fallback, because i'm interessted in replacing the (stupid, ugly) loading scenario for applets. it got a lot better with 1.4.2 and the option to add your own image (as we have it now with processing). bit still it's not really nice and some browsers ignore it (or is it the java-plugin ignoring it?). the tricky part with that is to know when the applet actually got started .. i mean start() had been called. anyway ...

F
Re: Javascript Embed Applet
Reply #7 - Jan 7th, 2007, 3:28pm
 
I've discovered that Flash won't store more than 132 chars per href on a textField. So I won't be calling opengl applets from my folio. But it's all online and appears to work now.

My thought for the loading issue is that you would have to go the AJAX style route. Perhaps hide the applet {display:none} and fire a js on setup() (but as with the other thread this is problematic). The major problem in using js to mount a loading screen is that you need to stagger the commands, and I'm not sure how to poll an applet from js. Unless of course there is a way to mount one Java applet on top of another, then you would make a non-Processing applet and import the processing file (but if that were possible we would have it by now). Of course if you have Flash on the page there is a sneaky browser-cache trick you could try. Have the Flash inform about the download of files and when they are cached the Java should just pop on to the page.

Personally this route just smacks of me working on my portfolio interface more. And the last project I worked on this long was a painting. That at least was relaxing.
Re: Javascript Embed Applet
Reply #8 - Jan 9th, 2007, 10:34pm
 
hi aaron,

thanks for you thoughts! i've been researching a lot and here is what i have so far (including a solution):

css, "display:none" : won't work. some browsers don't load things that are hidden (kinda makes sense).

applet in applet is an option. though you need to alter the applet's code, which i don't want to, because it's not compatible with "old" applets (think builtwithprocessing.org).

flash is not an option. don't want to have too many elements depend on each other ... and i tried a similar trick by having the browser preload the jars as images. it won't work since the java-plugin has it's own cache and therefore is gonna redownload the files anyway. another drawback with this is, that it won't start the VM before inserting the applet-tag into the page.
same goes for requests via ajax .. see here (using famous prototype.js).

the best solution i came up with so far:
for each jar have an applet outside the browser window load a fake/nonexistant class with the jar as archive. this preloads the jars into the plugin-cache and starts the VM. had to change your appletobject a little.
here's the test-page (don't forget to clear the java-plugins cache before reload).

works for: Firefox2.0(mac/win), Safari2.0, Opera9.0(mac), IE 5.01-6.0 (win), NS 7.2(win)
some browsers (NS, Opera8.5+9.1(win)) don't get the status of the preloader-applet and just run thru the loop. won't hurt there ..

F
Re: Javascript Embed Applet
Reply #9 - Jan 10th, 2007, 3:19pm
 
I'm only on WinXP but it doesn't work on my machine. I tried IE(6.0) and it froze and I got this from my coffee cup icon:
Code:

load: class loadFakeClass not found.
java.lang.ClassNotFoundException: loadFakeClass
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
at sun.applet.AppletClassLoader.getBytes(Unknown Source)
at sun.applet.AppletClassLoader.access$100(Unknown Source)
at sun.applet.AppletClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 10 more
Exception in thread "Thread-5" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-loadFakeClass" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

And I had to shut down IE from the Task Manager.

In Firefox(1.5) it started loading then got stuck. The oddest thing was that my middle mouse button stopped working (I use it to scroll a lot). Now it won't load in Firefox at all and I get the same console output. (Plus I have to force-quit Firefox).

That percentage could easily have an additional <div> that would be a load bar. You would just have to js it's size, perhaps even use some scrolling .gif as a background for an animated bar.

I've got a mate on Linux. When he comes online I'll ask him to test the url. Could be I've shafted Java mid-load or just my machine being crap. I hope so.
Re: Javascript Embed Applet
Reply #10 - Jan 10th, 2007, 4:05pm
 
jep. loadFakeClass is intentionally not there ... since JS can't know what's inside the jar.
the exception i'm getting (winxp, ie6, java 1.4.2) is:
Code:

java.lang.ClassNotFoundException: loadFakeClass
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
at sun.applet.AppletClassLoader.getBytes(Unknown Source)
at sun.applet.AppletClassLoader.access$100(Unknown Source)
at sun.applet.AppletClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 10 more
Exception: java.lang.ClassNotFoundException: loadFakeClass

which is ok, this basicly just says that the applet could not start 'cause the class was not there.

the stalling (FF1.5) might be due to the 2.3mb jar .. i janked some images in there to actually see some loading.

the freezing is happening because of a battle for resources between the java-plugin and javascripts setTimeout() ... i'm trying to let the browser catch some air inbetween each jar it loads, but still it's a little problematic.

i'm testing on WinXP, Debian Sarge on VitualPC.
Re: Javascript Embed Applet
Reply #11 - Jan 11th, 2007, 1:04pm
 
I have to restart my machine between tries. Still no luck.

My friend on Linux tried it and he said Firefox went a little mad, but no applet either (and I had him testing the appletobject.js last week - that went over smoothly).

I'll keep an eye out for solutions.
Re: Javascript Embed Applet
Reply #12 - Jan 11th, 2007, 8:21pm
 
yep. sorry about that ...

i'm looking into other ways to do it, will report back once i have something new.

F
Re: Javascript Embed Applet
Reply #13 - Jan 11th, 2007, 11:17pm
 
check:
http://bezier.de/xhtml/appletobject.html

F
Re: Javascript Embed Applet
Reply #14 - Jan 12th, 2007, 8:06pm
 
Success on WinXP and Linux. Whahey! Cheesy
Pages: 1 2