最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

using javascript variable inside the java function - Stack Overflow

programmeradmin2浏览0评论

wanted a solution on how to access the javascript variable inside the java scope. In the below code, <%=toppings[k]%> k is a javascript variable how can i access that, I am getting a message as k is not known to JVM

function value_transitAccountCounter(i){
    alert('wele -->'+i);
    for(var k=0;k<5;k++){
        if(i==k){
            return '<%=toppings[k]%>';
        }
    }
}

wanted a solution on how to access the javascript variable inside the java scope. In the below code, <%=toppings[k]%> k is a javascript variable how can i access that, I am getting a message as k is not known to JVM

function value_transitAccountCounter(i){
    alert('wele -->'+i);
    for(var k=0;k<5;k++){
        if(i==k){
            return '<%=toppings[k]%>';
        }
    }
}
Share Improve this question asked May 9, 2011 at 8:19 gmhkgmhk 16k29 gold badges91 silver badges112 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Your Java code is running on the server. Your JavaScript code is running on the client. So you can't use k on the server, it doesn't exist until the page is finished, sent to the browser, and the JavaScript therein executed.

From your code, it looks like you just need to access the strings stored in the server-side toppings array. You have two choices:

  1. Output the entire toppings array to the page's script as a JavaScript array. Then you can just use it client-side.

  2. Have your value_transitAccountCounter function make an ajax call to your server-side code, asking what the value of toppings[i] is. You'll need to change value_transitAccountCounter to accept a callback to use to return the value rather than actually returning it, because the ajax call will be asynchronous (value_transitAccountCounter will return before the ajax call pletes and thus, before you have the value to return). (It's possible to make synchronous ajax calls, but it's a very, very bad idea and you're better off pretending it isn't possible. It pletely locks up the UI of most browsers while the call is executing, incredibly irritating for users.)

You can't access a JavaScript variable from JSP. The JavaScript runs on the client side. The JSP runs on the server side. You could write something like this instead:

function value_transitAccountCounter(i) {
    alert('wele -->'+i);
    <% for (int k = 0; k < 5; k++) { %>
        if (i == <%= k %>) {
            return '<%= toppings[k] %>';
        }
    <% } %>
    }
}

The JSP runs on the server. It outputs some content. The content is sent to the browser. The browser then executes code.

You cannot directly municate between client side and server side code.

Since the JSP code runs first, it is easy to get data from the Java to the JavaScript (you just generate the JavaScript dynamically), however you want to go the other way.

In order to get data from a client side JavaScript variable to a server side JSP variable you must issue a new HTTP request by:

  • Setting location.href
  • Submitting a form
  • Using XMLHttpRequest
  • etc

The request needs to include the data you want to pass.

Crowder is right: you cannot intergrate your javascript in order to view java variable, but you can do the opposite, generally speaking, there is always a way to invert your problem in order to let your javascript code read java variable. Think about it.

Good luck

发布评论

评论列表(0)

  1. 暂无评论