kappa
|
 |
«
Posted
2009-03-01 15:16:23 » |
|
I was looking for a reliable way to detect if the java plugin is available on a browser, i've had a look at various javascript methods and sun's deployJava.js but they all seem a little unreliable especially on browser like safari and konqueror where no information is reported at all from javascript. I've since found the only reliable way is to actually have a java applet launch and report back that it is available. Thought i'd post the method here in case if it is useful to others. java code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.applet.Applet; import netscape.javascript.JSObject;
public class Test extends Applet { JSObject win; public void init() { win = JSObject.getWindow(this); Object[] param = { System.getProperty("java.version") }; win.call("javaSupported", param); }
} |
html/javascript code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>
<body onLoad="checkJava()">
<script type="text/javascript">
var javaFound = false;
function javaSupported(version) { javaFound = true; var gamebox=document.getElementById('appletbox'); gamebox.innerHTML = 'Java version ' + version + ' available'; }
function javaNotFound() { if (!javaFound) { var appletbox=document.getElementById('appletbox'); appletbox.innerHTML = 'Java Not Found'; } }
function checkJava() { setTimeout('javaNotFound();', 10000); } </script>
<div id="appletbox">Checking if Java Available...<applet code="Test" width="1" height="1" MAYSCRIPT/></div>
</body> </html> |
What the code does is wait 10 seconds for the applet to report back through liveconnect that it is available, if it does not report back it presumes that java is not available. Another thing that makes this faster than using a full applet is that the innerHTML quickly removes the applet once it has reported back and avoids any further slow downs from the applet that the paint methods, etc bring. You can just change this if you are using a full applet to leave applet there instead of removing it. example http://kappa.javaunlimited.net/temp/testjava.htmlworks for me on all the browsers and platforms i've tested on IE6,IE7,Opera,Safari,Firefox,Chrome, Konqueror on windows and linux.
|
|
|
|
|
h3ckboy
|
 |
«
Reply #1 - Posted
2009-03-01 16:05:47 » |
|
It works, nice job.
I tried it on IE(wich ahs java)
and chrome wich doesnt sadly:(
and both gave the correct answer back.
good job
|
|
|
|
|
Riven
|
 |
«
Reply #2 - Posted
2009-03-01 16:56:39 » |
|
New applets grab the focus.
If you're filling a HTML form, and the applet is launches, all key events will go to the applet.
This is very confusing for the user.
It will also freeze the browser for a couple of seconds, in versions under u10.
And there are 101 liveconnect bugs.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
pjt33
|
 |
«
Reply #3 - Posted
2009-03-01 23:37:23 » |
|
New applets grab the focus.
If you're filling a HTML form, and the applet is launches, all key events will go to the applet.
This is very confusing for the user.
It will also freeze the browser for a couple of seconds, in versions under u10.
And there are 101 liveconnect bugs.
Including, IME.
|
|
|
|
|
Jono
|
 |
«
Reply #4 - Posted
2009-03-02 01:52:59 » |
|
It more than just freezes.. it consistently, completely crashes firefox for me. I'm not sure what the exact cause is.
That's with the 1.6.0_10-b33 plugin, running on OpenSolaris 2008.11
|
|
|
|
|
bienator
Senior Member   
OutOfCoffeeException
|
 |
«
Reply #5 - Posted
2009-03-02 03:05:44 » |
|
http://people.fh-landshut.de/~mbien/cafebabe.htmlone method uses the deployment toolkit the other via mime types. It never happened to me that both didn't work (but I haven't tested konqueror). The JavaFX deployment toolkit recently got the mimetype querie too as fallback.
|
|
|
|
kappa
|
 |
«
Reply #6 - Posted
2009-03-02 07:52:46 » |
|
http://people.fh-landshut.de/~mbien/cafebabe.htmlone method uses the deployment toolkit the other via mime types. It never happened to me that both didn't work (but I haven't tested konqueror). The JavaFX deployment toolkit recently got the mimetype querie too as fallback. both methods there fail for me in opera.
|
|
|
|
|
Riven
|
 |
«
Reply #7 - Posted
2009-03-02 08:01:06 » |
|
Yup, also fails for me in Opera.
Then I again, I never managed to get LiveConnect to work in Opera, but given the marketshare of Opera, it's not top priority for me.
Opera doesn't recognize the "javascript:" protocol either, which behaves as eval(String) in MSIE and FF.
|
|
|
|
kappa
|
 |
«
Reply #8 - Posted
2009-03-02 08:35:07 » |
|
The two scripts also fail in Safari, it just goes to show the reliabilty of Suns deployment toolkit, besides it rather tedious and difficult to adjust/update the javascript each time a new browser comes out or for every browser thats out there. However the above applet code works on both safari and opera 
|
|
|
|
|
kappa
|
 |
«
Reply #9 - Posted
2009-03-02 08:48:14 » |
|
New applets grab the focus. If you're filling a HTML form, and the applet is launches, all key events will go to the applet.
True but hopefully it won't be used on a page that has a html form or if it does the applet will have loaded and closed before the user has a chance to focus the html form. Besides you could probably get focus back using javascript. It will also freeze the browser for a couple of seconds, in versions under u10.
Since this is very small class the browser freeze is smaller than that of when using a big jar and since the applet is closed before or when the init() method finishes it futher reduces the delay (thus not reaching the paint methods), however there is still a small delay just not as much as normal applets. And there are 101 liveconnect bugs.
Yes thats true, however this method uses very little liveconnect (only one method) and most of the bugs relate to browser to plugin communication (javascript to java), this is only plugin to browser communication (java to javascript). This should hopefully avoid most of those bugs especially some of those browser specific ones (for example with firefox).
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
zammbi
|
 |
«
Reply #10 - Posted
2009-03-02 09:23:49 » |
|
It works, nice job.
I tried it on IE(wich ahs java)
and chrome wich doesnt sadly:(
and both gave the correct answer back.
good job
Chrome worked for me.
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2009-03-02 09:30:23 » |
|
True but hopefully it won't be used on a page that has a html form or if it does the applet will have loaded and closed before the user has a chance to focus the html form. Besides you could probably get focus back using javascript.
The browser freeze is so nasty, that my approach used to be to load the applet after X seconds, under the assumption that people where reading the page at that time. But, man... that did backfire. Since this is very small class the browser freeze is smaller than that of when using a big jar and since the applet is closed before or when the init() method finishes it futher reduces the delay (thus not reaching the paint methods), however there is still a small delay just not as much as normal applets.
Unfortunately not. Even a pixel that is 1x1 px, and renders 1 color (for debugging) simply freezes the browser for up to 10 seconds on slower PCs with pre u10 Java versions. Yes thats true, however this method uses very little liveconnect (only one method) and most of the bugs relate to browser to plugin communication (javascript to java), this is only plugin to browser communication (java to javascript). This should hopefully avoid most of those bugs especially some of those browser specific ones (for example with firefox).
You only need 1 bug, and it stops working. It's sad. It's just that there is NO reliable way to figure out whether Java is supported in the browser, without annoying the end user.
|
|
|
|
h3ckboy
|
 |
«
Reply #12 - Posted
2009-03-02 13:53:29 » |
|
The reason I dont have java is tath I dont have admin users.... so sad. I code on a school comp. I am gunna have to cmpy a lot of crap off when I give it back in 3 years  . will take a while ot get off all of my crap, my java folder is currently 934 megabytes. that does include jdk and compilers though. back on topic  it really takes 10 seconds to load a 1x1 screen. so does a 10x10 take 100 seconds, or is it jsut the fact that is ti s a n applet.
|
|
|
|
|
Riven
|
 |
«
Reply #13 - Posted
2009-03-02 18:55:18 » |
|
Yes, a 800x600 applet takes 1333 hours to load, never noticed?
|
|
|
|
bienator
Senior Member   
OutOfCoffeeException
|
 |
«
Reply #14 - Posted
2009-03-02 19:58:50 » |
|
Yup, also fails for me in Opera.
Then I again, I never managed to get LiveConnect to work in Opera, but given the marketshare of Opera, it's not top priority for me.
Opera doesn't recognize the "javascript:" protocol either, which behaves as eval(String) in MSIE and FF.
i am a js noob, this was actually my first script i've written so far. Could you update it in a way that opera could interpret it without breaking it for IE and FF?
|
|
|
|
bienator
Senior Member   
OutOfCoffeeException
|
 |
«
Reply #15 - Posted
2009-03-02 20:17:56 » |
|
i updated the script to use javafx deployment toolkit.
|
|
|
|
kappa
|
 |
«
Reply #16 - Posted
2009-03-02 20:47:01 » |
|
i updated the script to use javafx deployment toolkit. the javafx script work in opera now but still fails in safari.
|
|
|
|
|
bienator
Senior Member   
OutOfCoffeeException
|
 |
«
Reply #17 - Posted
2009-03-02 21:34:17 » |
|
what do you get as result? "0 - java not enabled" or a empty box? also could you try this page? it uses a different version of the dtfx.js
|
|
|
|
kappa
|
 |
«
Reply #18 - Posted
2009-03-02 21:54:31 » |
|
what do you get as result? "0 - java not enabled" or a empty box?
I get an empty box and also same result on konqueror. looking at the code for dtfx.js i'm very impressed with it, it is much more advance and uptodate when compared with the standard deployJava.js as it has support for much more browsers like opera and chrome. Not sure why sun doesn't just use this script for both deployment toolkits and kill the older and less compatible deployJava.js as for using javascript to detect java on safari, I don't think it can be done with pure javascript since the browser doesn't support it, the best that can be done is just check if it is safari and if so then just try run the applet whether java is present or not.
|
|
|
|
|
bienator
Senior Member   
OutOfCoffeeException
|
 |
«
Reply #19 - Posted
2009-03-02 22:20:24 » |
|
Not sure why sun doesn't just use this script for both deployment toolkits and kill the older and less compatible deployJava.js
they use deployJava.js from within dtfx.js. I guess the only reason to have two scrips is simple because JavaFX development runs asynchronously and more rapidly compared to JRE development. Its easier to introduce api incompatibilities in JavaFX as in Java, a separated deployment script simplifies things. I see no reasons which prevent you from using dtfx.js for plain java deployment but i would prefer a merged document also.
|
|
|
|
DzzD
|
 |
«
Reply #20 - Posted
2009-03-04 12:59:19 » |
|
Yes, a 800x600 applet takes 1333 hours to load, never noticed?
lol detecting java is really something awefulll...
|
|
|
|
h3ckboy
|
 |
«
Reply #21 - Posted
2009-03-04 13:40:56 » |
|
for the record I was just chucking a question out so taht the post would be on topic  and I was just clearing up my understanding of the situation.
|
|
|
|
|
|