Passing variable from JSP to Javascript

后端 未结 5 1000
梦谈多话
梦谈多话 2020-12-03 21:52

I know there is already questions about that, but I just can´t simply get this work, I have a JSP file with a java variable in it:

String test = \"Hello\";

相关标签:
5条回答
  • 2020-12-03 22:22

    I know this one is old, but this worked for me:

    var javaScriptVar="<%out.print(javaString);%>";
    

    you need to make sure that if you are using an external js file (out of the jsp file), the above line has to be before the "include" script tag. for example this is the jsp file:

    var javaScriptVar="<%out.print(javaString);%>";
    <script src="some_location/test.js"></script>
    
    0 讨论(0)
  • 2020-12-03 22:24

    You can pass the value by calling some methods in html part..

    <input type="submit" value="view" onclick="callpro('<%= varname %>')" />
    
    0 讨论(0)
  • 2020-12-03 22:29
    var jsvariable="<%=test%>";
    
    0 讨论(0)
  • 2020-12-03 22:38

    The best way to do it is to use something like following in your javascript code;

    var myvar = '${jspvar}';
    
    0 讨论(0)
  • 2020-12-03 22:43

    One thing to note is that you could namespace those variables in this way:

    var MYAPP.javaScriptVar="<%out.print(javaString);%>";

    The technique is from "Javascript: The Good Parts" Book.

    0 讨论(0)
提交回复
热议问题