return value from JSP to Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dsimonneau

    #1

    return value from JSP to Javascript

    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="Javas cript1.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
  • Chris Riesbeck

    #2
    Re: return value from JSP to Javascript

    In article <5ba794fb.03090 50816.10e62598@ posting.google. com>,
    dsimonne@ptc.co m (dsimonneau) wrote:
    [color=blue]
    > 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="Javas cript1.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[/color]

    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><ti tle>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><ti tle>Main Page</title>
    <script language="javas cript">
    function getData() { top.data.locati on.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="getDat a()">
    </form>
    </body>
    </html>

    File 3: data.jsp -- uses JSP to set Javascript variables

    <html><head>Dat a page</title>
    <script language="javas cript">
    var date = "<%= new java.util.Date( ) %>";
    </script>
    </head>
    <body onload="top.mai n.dataLoaded()" >
    <p>Move along, nothing to see here.</p></body>
    </html>

    Comment

    • Code Ronin

      #3
      Re: return value from JSP to Javascript

      dsimonne@ptc.co m (dsimonneau) wrote in message news:<5ba794fb. 0309050816.10e6 2598@posting.go ogle.com>...[color=blue]
      > I would like to call a JSP from a Javascript and get a result on my
      > javascript[/color]

      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.create Element ( "SCRIPT" );
      newScript.src = "alert.txt" ;
      document.body.a ppendChild ( newScript );
      }
      function destroyScript ( ) {
      if ( newScript != null ) {
      document.body.r emoveChild ( 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.

      Comment

      Working...