Getting jsp value in javascript file

18,339

Solution 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;

Solution 2

use this...

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

in your js file use

var sfl=document.getElementById("sfl").value; 
Share:
18,339
bdfios
Author by

bdfios

Updated on June 18, 2022

Comments

  • bdfios
    bdfios almost 2 years

    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
    bdfios about 11 years
    It didn't work. Please note that I am using an external javascript.
  • 11684
    11684 about 11 years
    Then put the code from my answer in your external javascript file (after removing the script tags). @bdfios