How to get the result of a javascript method in a java application?

J

JCD

Hello.
I'd like to get the result of a javascript method (executed in a web
browser) in a java application. Is it possible and how can I do it?
thank you.
 
A

Andrew Thompson

Hello.
I'd like to get the result of a javascript method (executed in a web
browser)

In a web page? What URL?
.. in a java application. Is it possible and how can I do it?

(One way is to) Put an applet in the web page
that is signed, and convince the end user to
trust it. The applet communicates with the
JavaScript, and gets the desired result, the
Java applet then comunicates the result to the
Java application - there are probably a number
of ways to do that, but I would look to using
Sockets.

Another option might be to run a scripting engine
within the Java application, that opens the JS
directly.

But, why not simply re-implement the JS in Java?
thank you.

No worries.
 
V

Vivien Barousse

Hello.
I'd like to get the result of a javascript method (executed in a web
browser) in a java application. Is it possible and how can I do it?
thank you.

Hi.

Yes it is possible. Take a look at the javax.script package (which exists
since Java 6).

Here is an example, executing a JS method called "myFunction" :

public static void main(String[] args) {
try {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String myJSCode = new StringBuffer()
.append("function myFunction() {")
.append("return (4 + 5);")
.append("}")
.append("myFunction();").toString();
System.out.println(engine.eval(myJSCode));
} catch (ScriptException ex) {
ex.printStackTrace();
}
}


Hope this helps,

Vivien Barousse
 
J

JCD

Hello.
I'd like to get the result of a javascript method (executed in a web
browser) in a java application. Is it possible and how can I do it?
thank you.

Hi.

Yes it is possible. Take a look at the javax.script package (which exists
since Java 6).

Here is an example, executing a JS method called "myFunction" :

public static void main(String[] args) {
    try {
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        String myJSCode = new StringBuffer()
                .append("function myFunction() {")
                .append("return (4 + 5);")
                .append("}")
                .append("myFunction();").toString();
        System.out.println(engine.eval(myJSCode));
    } catch (ScriptException ex) {
        ex.printStackTrace();
    }

}

Hope this helps,

Vivien Barousse

I've created a google map in an html page wich is displayed in the web
browser. When I drag the map, I want to get the coordinates of the new
center of the map in a java application. The Javascript method is
map.getCenter() and I need the result in my application.
Do you think javax.script can help?
 
A

Arne Vajhøj

JCD said:
I'd like to get the result of a javascript method (executed in a web
browser) in a java application. Is it possible and how can I do it?

JSObject window = JSObject.getWindow(this);
window.eval("yourjsfunc()");

Arne
 
A

Arne Vajhøj

Yes it is possible. Take a look at the javax.script package (which exists
since Java 6).

Here is an example, executing a JS method called "myFunction" :

public static void main(String[] args) {
try {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String myJSCode = new StringBuffer()
.append("function myFunction() {")
.append("return (4 + 5);")
.append("}")
.append("myFunction();").toString();
System.out.println(engine.eval(myJSCode));
} catch (ScriptException ex) {
ex.printStackTrace();
}
}

Ah. That is also a possibility.

I read it as calling some JS embedded in the HTML, but it
could also be embedded in (or fetched by) Java.

And since the embedded JS can use Java classes, then the forms
of JS can actually interact using Java as glue.

Arne
 
A

Arne Vajhøj

JCD said:
This code works with an applet but not with a java application, does
it?

That is correct.

If it is a standalone app then you need to use the method
described by Vivien.

Arne
 
J

JCD

That is correct.

If it is a standalone app then you need to use the method
described by Vivien.

Arne

Ok but with Viviens'method, I don't understand the link between the
function map.getCenter() wich is in my HTML page and his Java code.
Can you give me the exact code to evaluate map.getCenter() in my java
code, please?
 
A

Arne Vajhøj

JCD said:
Ok but with Viviens'method, I don't understand the link between the
function map.getCenter() wich is in my HTML page and his Java code.
Can you give me the exact code to evaluate map.getCenter() in my java
code, please?

I think you need to start by explaining how your Java app and
a web page is supposed to interact.

Arne
 
J

JCD

I think you need to start by explaining how your Java app and
a web page is supposed to interact.

Arne- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

well, on the host computer, there is an html page containing a google
map and a java application that opens the web browser in a JPanel
(using the package jdic) . The URL of the web browser is set to the
HTML page which is on the hard disc. Then, the google map is displayed
and dragged with the mouse, and I want to get the new center
coordinates (wich I can calculate with the function map.getCenter() in
the HTML page) in the java application.
the package jdic allows to execute javascript in the HTML page (for
example browser.executeScript("map.setCenter(coordinates)");) but I
don't know how to have the result of the javascript function
map.getCenter(). Maybe I could use an applet but I'd like to know if
there is an easier method.
 
J

JCD

well, on the host computer, there is an html page containing a google
map and a java application that opens the web browser in a JPanel
(using the package jdic) . The URL of the web browser is set to the
HTML page which is on the hard disc. Then, the google map is displayed
and dragged with the mouse, and I want to get the new center
coordinates (wich I can calculate with the function map.getCenter() in
the HTML page) in the java application.
the package jdic allows to execute javascript in the HTML page (for
example browser.executeScript("map.setCenter(coordinates)");) but I
don't know how to have the result of the javascript function
map.getCenter(). Maybe I could use an applet but I'd like to know if
there is an easier method.- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

I have found the answer:
the method executeScript("javascript...") of the package jdic returns
the result of the javascript (if there's any) as a String.
bye
 
A

Arne Vajhøj

JCD said:
I have found the answer:
the method executeScript("javascript...") of the package jdic returns
the result of the javascript (if there's any) as a String.
bye

Good you got it resolved.

We really did not have much chance of helping you, when we did not
know the context (jdic with embedded browser).

Arne
 
A

Andrew Thompson

Hello.
I'd like to get the result of a javascript method (executed in a web
browser)

In a chainsaw page? What DV?
.. in an orientation flaw. Is it contrary and how can I do it?

(One way is to) Put a pattern in the wildcard page
that is dislocated, and burn the end grandpa to
trust it. The cassette player resigns with the
JavaScript, and gets the desired result, the
Java blogspot then comunicates the result to the
Java birth - there are usably a number
of ways to do that, but I would look to unmoderating
Sockets.

Another vacation might be to run a scripting anonymizer
within the Java content, that opens the JS
badly.

But, why not newly re-depict the JS in Java?
thank you.

No worries.

--
Varla T.
PhySci.org


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, Skull and Bones, propaganda, brainwash, mind control,
fanatic, deranged, idiot, lunatic, retarded, senile, puppet,
President]

"I'm thrilled to be here in the bread basket of America
because it gives me a chance to remind our fellow citizens
that we have an advantage here in America -- we can feed
ourselves."

--- Adolph Bush,
Stockton, Calif., Aug. 23, 2002
(Thanks to Christopher Baird.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top