[英]Pass a variable from .jsp page to .js
I am working on a .jsp page where depending on what header comes from a request it appears a different div from a .js file I add a second header and I would like 我正在.jsp页面上工作,其中取决于来自请求的标头,它看起来与.js文件不同,因此我添加了第二个标头,我想
if (info.getLightHeader()!=null)
the 的
<div id="lightHeader" class="juices">
to appear(the code will be at the .js file) 出现(代码将在.js文件中)
Is this possible to be?Thank you in advance 这可能是吗?谢谢
You can use the jsp script tags inside javascript: 您可以在javascript中使用jsp脚本标签:
<%
String paramLightHeader = "";
if (info.getLightHeader()!=null){
paramLightHeader=info.getLightHeader();
}
%>
use the variable in javascript: 在javascript中使用变量:
Update: as per your provide code you need this: 更新:根据您提供的代码,您需要:
<input type="hidden" name="lightParamHeader" value='<%= paramLightHeader %>' >
<c:out value="${lightParamHeader}" />
Instead of having a separated .js file, you could have a .jsp file with javascript tags. 除了使用单独的.js文件之外,您还可以使用带有javascript标签的.jsp文件。
scripts.jsp scripts.jsp
<script type="text/javascript">
var foo = '<%= "Hi" %>'
</script>
您可以在JSP页面上声明一个JavaScript变量,该变量可以在所有js文件中使用,并且可以使用表达式语言传递该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.