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

I have two jsp files.In one of them i have a function which has a variable, say x, that I need to use

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098835a4611149.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信