return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>Calling JavaScript function in a .js file from Java - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling JavaScript function in a .js file from Java - Stack Overflow

programmeradmin1浏览0评论

What is the best way to call a JavaScript function in a different .js file from a Java file?

Say, I have a String like so in a Java file:

String test = "[name1, name2, name2]";

What I am trying to do is the following:

  1. Convert the Java String into a JavaScript array
  2. Pass the JavaScript array to a JavaScript function in a different .js file.

Thanks, Sony

What is the best way to call a JavaScript function in a different .js file from a Java file?

Say, I have a String like so in a Java file:

String test = "[name1, name2, name2]";

What I am trying to do is the following:

  1. Convert the Java String into a JavaScript array
  2. Pass the JavaScript array to a JavaScript function in a different .js file.

Thanks, Sony

Share Improve this question asked Mar 4, 2012 at 0:05 sonysony 1,58710 gold badges37 silver badges51 bronze badges 3
  • 1 JS runs on the client side, Java runs on the server's side. The server-side does not "pass" parameters to the client-side, the server-side generates the client-side. So I don't understand the issue. Can you tell us more about what you're trying to do ? – Nir Alfasi Commented Mar 4, 2012 at 0:17
  • @alfasin Java runs on the server's side Java also could be a java applet java.sun./products/plugin/1.3/docs/jsobject.html – Cheery Commented Mar 4, 2012 at 2:42
  • 1 @Cheery you're right. But I have a feeling that Sony didn't refer to applets. – Nir Alfasi Commented Mar 4, 2012 at 4:25
Add a ment  | 

2 Answers 2

Reset to default 6

If you want to use JavaScript in a Java application, you can use Rhino.

If you want to call client side JavaScript form a serverside Java web application, you can use DWR's Reverse Ajax.

Good luck!

If you want to integrate Javascript and Java, then you can use Rhino. See the example below for a better prehension:

test.js

function test(array){
    for(var i in array){
        out.println(array[i]);
    }
}

RhinoTest.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.Scriptable;

public class RhinoTest {

    public static void main(String[] args) {

        Context ctx = new ContextFactory().enterContext();

        Scriptable scope = ctx.initStandardObjects();

        try {
            Object out = Context.javaToJS(System.out, scope);
            scope.put("out", scope, out);

            FileReader fr = new FileReader("test.js");
            ctx.evaluateReader(scope, fr, "<cmd>", 1, null);
            fr.close();

            String[] strArray = {"name1","name2","name3"};
            Object jsArray = Context.javaToJS(strArray, scope);
            scope.put("jsArray", scope, jsArray);

            ctx.evaluateString(scope, "test(jsArray)", "<cmd>", 1, null);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Context.exit();
    }
}
发布评论

评论列表(0)

  1. 暂无评论