return value from JSP to Javascript

D

dsimonneau

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

Thanks for any help

Denis
 
C

Chris Riesbeck

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>
 
C

Code Ronin

I would like to call a JSP from a Javascript and get a result on my
javascript

Here is an old example of dynamically creating <SCRIPT> elements. In
this example, it uses a text file (alert.txt) as the source of the
script, but you can replace the value with a JSP.

// ---- start of code
// ---- loader.htm

<html>
<head>
<script type="text/javascript">
var newScript;
function init ( ) {
createScript ( );
}
function createScript ( ) {
newScript = document.createElement ( "SCRIPT" );
newScript.src = "alert.txt";
document.body.appendChild ( newScript );
}
function destroyScript ( ) {
if ( newScript != null ) {
document.body.removeChild ( newScript );
}
newScript = null;
}
</script>
</head>
<body onload="init()">
</body>
</html>

// ---- alert.txt

alert ( "this is from a text file" );

// ---- end of code

All you have to make sure of is that the URL used to supply the src
MUST return executable JavaScript. It really does not matter what the
URL is (JSP, ASP, text, etc.) as long as its JS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top