I have the following code in abc.jsp:
<%@page import="soundcap.Soundcap"%>
<%
Soundcap cfl = new Soundcap();
var sfl = cfl.playFile();
%>
I need the value of sfl in an external javascript file (jcode.js). How can I get this value (sfl) from jsp in javascript?
I have the following code in abc.jsp:
<%@page import="soundcap.Soundcap"%>
<%
Soundcap cfl = new Soundcap();
var sfl = cfl.playFile();
%>
I need the value of sfl in an external javascript file (jcode.js). How can I get this value (sfl) from jsp in javascript?
Share Improve this question asked Apr 22, 2013 at 19:03 bdfiosbdfios 6676 gold badges18 silver badges29 bronze badges 6-
Just
out.print()
it directly in the JS file. – 11684 Commented Apr 22, 2013 at 19:07 - Thanks, but I would like to get it into a variable like this var xy = ... what is ...? – bdfios Commented Apr 22, 2013 at 19:09
- Something like my answer? – 11684 Commented Apr 22, 2013 at 19:10
- possible duplicate of How to let JavaScript use the variable from server side? – BalusC Commented Apr 22, 2013 at 19:14
- Thanks everyone for your suggestions. Your suggestions make sense to me but this stuff isn't just working. I am going through the entire code right now to see why it isn't working. I think its probably from my end. Thanks! – bdfios Commented Apr 22, 2013 at 20:13
3 Answers
Reset to default 1use this...
<%
//get your sfl
%>
<input type="hidden" id="sfl" value="<%=sfl%>">
in your js file use
var sfl=document.getElementById("sfl").value;
Just place your JSP value in input tag like this:
<input type="hidden" id="value" value="${value}">
And get this value in JS like this:
var value = document.getElementById("value").value;
Very simple:
<%
//get your sfl
%>
<script>
var xyz = <% out.print(sfl); %>;
</script>