6

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?

bdfios
  • 657
  • 6
  • 17
  • 29
  • Just `out.print()` it directly in the JS file. – 11684 Apr 22 '13 at 19:07
  • Thanks, but I would like to get it into a variable like this var xy = ... what is ...? – bdfios Apr 22 '13 at 19:09
  • Something like my answer? – 11684 Apr 22 '13 at 19:10
  • possible duplicate of [How to let JavaScript use the variable from server side?](http://stackoverflow.com/questions/14348265/how-to-let-javascript-use-the-variable-from-server-side/14348580#14348580) – BalusC Apr 22 '13 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 Apr 22 '13 at 20:13
  • Your (you both) suggestions are quite valid. True, it wasn't working in my external .js file, but when I copied the script into index.jsp, it works. Thanks. – bdfios Apr 25 '13 at 14:20

3 Answers3

1

use this...

<%
//get your sfl
 %>
  <input type="hidden" id="sfl" value="<%=sfl%>">

in your js file use

var sfl=document.getElementById("sfl").value; 
rahul maindargi
  • 5,359
  • 2
  • 16
  • 23
1

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;
zygimantus
  • 3,649
  • 4
  • 39
  • 54
0

Very simple:

<% 
//get your sfl
%>

<script>
var xyz = <% out.print(sfl); %>;
</script>
11684
  • 7,356
  • 12
  • 48
  • 71