最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting jsp value in javascript file - Stack Overflow

programmeradmin2浏览0评论

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
 |  Show 1 more ment

3 Answers 3

Reset to default 1

use 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>
发布评论

评论列表(0)

  1. 暂无评论