In article <5b**************************@posting.google.com >,
ds******@ptc.com (dsimonneau) wrote:
Hello all,
I would like to call a JSP from a Javascript and get a result on my
javascript
To call the JSP I'm doing like that :
<SCRIPT LANGUAGE="Javascript1.1">
function test()
{
var img = new Image();
img.src= 'http://myserver/jsp/test/test2.jsp';
}
It works fine, but I don't know how I could get a return value from
the JSP to the Javascript
One option: load JSP into an invisible frame.
Warning: the idea is simple but things get complicated fast.
Here's a short 3 file demo. Note the use of onload
to tell main.html when the data has been sent.
File 1: demo.html -- sets up the empty frame
<html><head><title>JSP Javascript Demo</title></head>
<frameset cols="0%, *">
<frame name="data" frameborder="0">
<frame name="main" src="main.html" frameborder="0">
</frameset>
</body>
</html>
File 2: main.html -- defines one function to ask for data,
another to respond when it's ready
<html><head><title>Main Page</title>
<script language="javascript">
function getData() { top.data.location.reload("data.jsp"); }
function dataLoaded() { alert("It is now " + top.data.date); }
</script>
</head>
<body>
<form>
<input type="button" value="For a good time..." onclick="getData()">
</form>
</body>
</html>
File 3: data.jsp -- uses JSP to set Javascript variables
<html><head>Data page</title>
<script language="javascript">
var date = "<%= new java.util.Date() %>";
</script>
</head>
<body onload="top.main.dataLoaded()">
<p>Move along, nothing to see here.</p></body>
</html>