Reputation: 308
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?
Upvotes: 1
Views: 12711
Reputation:
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>
Upvotes: 2
Reputation: 110
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
Upvotes: 0