2

を呼び出して、Java アプレットから JavaScript コードを呼び出すことができると読みました。

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

ただし、これを行うと、次のエラーが発生します。

java.net.MalformedURLException: unknown protocol: javascript

これを回避するにはどうすればよいですか?

4

2 に答える 2

4

URL クラスが javascript: を有効なプロトコルとして受け入れないため、あなたと同じ例外が発生します。

ただし、回避策があります。URL コンストラクターに URLStreamHandler を提供します。

例:

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
}
于 2008-10-15T12:33:15.387 に答える
2
    try {
        this.getAppletContext().showDocument(new URL("javascript:alert('hello world');"));
    }catch(Exception e) {
        e.printStackTrace();
    }

動作します!!

ブラウザでJavaScriptが有効になっていない可能性があります..ただの推測です

于 2008-10-06T09:58:51.473 に答える