0
  <script type="text/javascript">
    function start() {
        window['progress'] = setInterval(function() {
            var pbClient = PF('pbClient'),
            oldValue = pbClient.getValue(),
            newValue = oldValue + 10;
            pbClient.setValue(pbClient.getValue() + 10);

            if(newValue === 100) {
                clearInterval(window['progress']);
            }
        }, 100);
    }
  </script>

I want to call this function in my Java class.Actually this problem is the progress bar is in dialog.And I want to go another xhtml page after progress bar

dsh
  • 12,037
  • 3
  • 33
  • 51
Erhan
  • 108
  • 1
  • 10

3 Answers3

1

To call an external javascript functions you can use ScriptEngine.eval(java.io.Reader) and here is the documentation

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine Scr_engine = manager.getEngineByName("JavaScript");
// To read script file
engine.eval(Files.newBufferedReader(Paths.get("Full path of you JS file"), StandardCharsets.UTF_8));
Invocable invvoc = (Invocable) Scr_engine;
// To call the JS function from script file
invvoc .invokeFunction("yourFunctionName", "param");
Abder KRIMA
  • 3,418
  • 5
  • 31
  • 54
0

You can't call Javascript functions, which run in the browser, from Java code which runs on your server. You need to send messages over the network for interaction between your server and the client. I would look into Server-Sent Events.

dsh
  • 12,037
  • 3
  • 33
  • 51
  • Answer is simply incorrect. There are two direct answers to this question below. – Yuriy N. May 04 '17 at 12:52
  • @yurin Neither of the other answers are correct. While there are Javascript interpreters that can be embedded into a JVM to execute Javascript code, those do not handle HTML or XHTML pages. The question clearly shows a snippet of an (X)HTML page and specifically asks to navigate to another XHTML page when the progress is complete. This indicates that his Javascript code is in a web browser, and thus neither Rhino nor the new Nashorn is applicable. – dsh May 04 '17 at 13:09
  • I agree that question, when read carefully can be interpreted your way. But what actually happens (to me as well): someone googles "How to execute javascript function from Java Class", come to this page and see your answer. He easily be mislead. – Yuriy N. May 06 '17 at 10:55
  • Indeed, the summary is incomplete since it gives no indication of the context to which it applies. – dsh May 10 '17 at 12:56
0

Rhino is what you are looking for.

Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users.

Look here : How can I use Javascript in Java?

Community
  • 1
  • 1
Kashyap Kansara
  • 405
  • 4
  • 10