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?

6
  • Just out.print() it directly in the JS file. Commented Apr 22, 2013 at 19:07
  • Thanks, but I would like to get it into a variable like this var xy = ... what is ...? Commented Apr 22, 2013 at 19:09
  • Something like my answer? Commented Apr 22, 2013 at 19:10
  • possible duplicate of How to let JavaScript use the variable from server side? 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! Commented Apr 22, 2013 at 20:13

3 Answers 3

1

use this...

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

in your js file use

var sfl=document.getElementById("sfl").value; 
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

0

Very simple:

<% 
//get your sfl
%>

<script>
var xyz = <% out.print(sfl); %>;
</script>

2 Comments

It didn't work. Please note that I am using an external javascript.
Then put the code from my answer in your external javascript file (after removing the script tags). @bdfios

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.