简体   繁体   中英

Dynamic values of javascript accessed from other HTML

I hve a JSP file which has a text boxes with name param.when i click a button in the JSP form a function of a javascript is called. The values are trapped by the JS function using "document.forms[0].param.value". When i try to display the same value in the JSP by using alert feature the value gets displayed. But when i use a HTML and try accessing the value.i am not able to retrieve it ,meaning it is empty.But when i use a static value. The value is retrieved. I will enclose the JSP ,JS and HTML file code.Please help me .I worked on this issue for considerable time.

Here is the JSP code

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org    /TR/html4/loose.dtd">
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Insert title here</title>
   <script type="text/javascript" language="JavaScript" src="sample.js" > </script>
    </head>
   <body>
   <form action="formproc1" name="form1" method = "GET"   >
   <input type=text name = "param" value=""></input>
   <center> <input type="submit" value="submit"></input> </center>
   <input type="button" value="click" onclick="justshow()"></input>
   <input type="button"  value="click1" onclick="displaystr()"></input>
   </form>


   </body>
   </html>

Here is the javascript file sample.js

  function justshow()
 {
   str=document.forms[0].param.value;
  //document.print("hii");
   str1= str;
   alert(str);
  }
   function displaystr()
   {
str2=str1;
alert(str);

    }

   function display()
   {
     return "answer";
  return str;

   }

      Here is the HTML code which accesses  the sample.js file  


  <html>
  <head>
  <script type="text/javascript">
   function product(a,b)
   {
    return a*b;
   }
  </script>
    <script type="text/javascript" src ="C:\Documents and Settings\256160\workspace\simpleapp\WebContent\sample.js"> </script>
    </head>

    <body>
      <script type="text/javascript">
       document.write(product(1,5));
       document.write(display());//this does nt 
    </script>

   <p>The script in the body section calls a function </p>

    </body>
    </html>

Finally the question is THE DISPLAY FUNCTION MUST RETURN THE STRING ENTERED IN THE JSP TEXT BOX TO THE HTML .

I don't really get what you're trying to do with your code, the function display() is not doing anything, it's not assigning str anything. When you use the js file it doesn't save variables to your computer, each refresh will cause the variable to reset. If what you are trying to do is display the answer from the first page to the second then this is the wrong way to do this, JSP's are meant to be attached to java code that is suppose to process the form and output something to the response page. If you want a purely html form then you can do this:

<form action="formproc1" name="form1" method="GET">
   <input type="text" name="param"></input>
   <input type="submit" value="submit"></input>
</form>

and in the HTML response page you get the param through this:

<script>
   document.write(location.search.split("=")[1]);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM