java - Calling JS from Applet: MalformedURLException: unknown protocol: javascript - Stack Overflow

I read that you could call JavaScript code from a Java Applet by callingJApplet.getAppletContext().show

I read that you could call JavaScript code from a Java Applet by calling

JApplet.getAppletContext().showDocument( "javascript:alert('Hello World');" );

However, when I do this i get the following error:

java.MalformedURLException: unknown protocol: javascript

How do I work around this?

I read that you could call JavaScript code from a Java Applet by calling

JApplet.getAppletContext().showDocument( "javascript:alert('Hello World');" );

However, when I do this i get the following error:

java.MalformedURLException: unknown protocol: javascript

How do I work around this?

Share asked Oct 6, 2008 at 8:57 user20298user20298 1,8453 gold badges17 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I get the same exception as you do because of that the URL class does not accept javascript: as a valid protocol.

There is a workaround though; supply an URLStreamHandler to the URL constructor.

Example:

final URLStreamHandler streamHandler = new URLStreamHandler() {

    @Override
    protected URLConnection openConnection(URL u)
        throws IOException {
        return null;
    }

};

try {
    getAppletContext().showDocument(
        new URL(null, "javascript:alert('It works!');", streamHandler));
} catch (MalformedURLException me) {
    //log or whatever
}
    try {
        this.getAppletContext().showDocument(new URL("javascript:alert('hello world');"));
    }catch(Exception e) {
        e.printStackTrace();
    }

Works !!

Maybe the browser does not have javascript enabled.. just a guess

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744819867a4595549.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信