简体   繁体   中英

Can we pass a jsp variable to jQuery ready function

I a have a jQuery ready function given below, I need to pass a value to the myLocFn() . What I am asking is, as we are using ready function ,if it possible to pass a jsp value to myLocFn().

<script>  
$(document).ready(function(){
    $(".myDiv").myLocFn();     // need to pass a value here                  
});
</script>

I have included the js path as well <script src="myLocFn.js"></script> . Where the my js starts with,

(function($){
    $.fn.myLocFn= function(options) {
..... // need to get the value here

Write a JSP variable inside script section using server code:

<script type="text/javascript">

var jspVar="JSP_value"; // use expression tag in JSP here
</script>

Then refer in your jQuery code, just like a JavaScript variable, as u assigned ur JSP value to JS variable in above line

(function($){
    $.fn.myLocFn= function(options) {
    alert(jspVar); //here u can see 
}

Edit: Please make sure that jspVar should be defined before accessing. You can check this from the rendered HTML

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