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

javascript - Call a function defined in one JSP file in another JSP file - Stack Overflow

programmeradmin1浏览0评论

I have two jsp files. In one of them i have a function which has a variable, say x, that I need to use in another jsp file. So I made a variable , say y, outside the function and in that function, I called a serVar() function which sets y to x. Something like ...

<script>
.
.
.
var y;
function someFunction()
{
var x;
.
.
setVar(x);
}

function setVar(x)
{
y=x;
}

function getVar()
{
return y;
}
.
.
</script>

To use it in another jsp file, I also made a function like getVar() in the same file as shown above. How can I call this function getVar() in another jsp file?

I have two jsp files. In one of them i have a function which has a variable, say x, that I need to use in another jsp file. So I made a variable , say y, outside the function and in that function, I called a serVar() function which sets y to x. Something like ...

<script>
.
.
.
var y;
function someFunction()
{
var x;
.
.
setVar(x);
}

function setVar(x)
{
y=x;
}

function getVar()
{
return y;
}
.
.
</script>

To use it in another jsp file, I also made a function like getVar() in the same file as shown above. How can I call this function getVar() in another jsp file?

Share edited Jun 26, 2015 at 6:30 Ridhima asked Jun 26, 2015 at 6:00 RidhimaRidhima 3081 gold badge3 silver badges14 bronze badges 1
  • Including jsp into another jsp will result in execution jsp scriplets and/or other java codes of the included jsp. Ideally you should have your java script into external js and import js files using script tag in the jsp/html page. – user2575725 Commented Jun 26, 2015 at 6:23
Add a ment  | 

2 Answers 2

Reset to default 2

Including jsp into another jsp will result in execution jsp scriplets and/or other java codes of the included jsp. Ideally you should have your java script into external js and import js files using script tag in the jsp/html page.

However, if you still want to go for it, try using jsp:include.

<jsp:include page="your.jsp"/>  <!-- jsp with getVar() defined -->
<script>
  alert(getVar());
</script>

Or you can also use include directive:

<%@include file="your.jsp" %>  <!-- jsp with getVar() defined -->
<script>
    alert(getVar());
</script>

Move the function from the inline script block to a seperate file and the include the file in both of your jsp files, where you want use the like this:

<script src='<pathToFile>'></script>

The script must be included/run before you can use the function, so put it above the code, where you need the function

发布评论

评论列表(0)

  1. 暂无评论