简体   繁体   中英

Call jstl variables to javascript function

File name : Test.jsp

I declared a variable under jstl tag

<%

String daysToActivate = Someclass.method();

%>

I want to call this method in the following javascript fumction

//javascript function
function getDays(){



var x= daysToActivate;

alert(x);

}

// this alert message is not displaying.

please help

JavaScript doesn't know about Java variables declared in scriptlets and other places. It's because Java code is run and resolved on the server, and JavaScript is run in user's web browser.

To pass variable's value from Java to JavaScript put this script inside JSP page:

function getDays(){
    var x = <%= daysToActivate %>;
    alert(x);
}

Value of daysToActivate will be resolved on the server side, inserted into HTML file as text, and send to the user's web browser. Then on the client's side, from the web browser perspective this function will look like:

function getDays(){
    var x = 25;
    alert(x);
}

If daysToActivate is not an integer, then remember to use " :

function getDays(){
    var x = "<%= daysToActivate %>";
    alert(x);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM