views:

70

answers:

4

Please see following example code that takes a year and a date and displays the data back when submitted...

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript</title>   
</head>
<body>
<FORM NAME="myform" ACTION="" METHOD="GET">
Enter the year to start from (eg: 2006): <BR>
<INPUT TYPE="text" NAME="year" VALUE=""><P>
Enter starting day (eg: 1= 1st Jan): <BR>
<INPUT TYPE="text" NAME="day" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Go!" onClick="testResults(this.form)">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {

  var years = form.year.value;
  var days = form.day.value;



   for (var x = days; x <= 365; x++)
   {

        for (var y = years; y <= 2010; y++)

        {

        document.write('  ValueY='+y+'ValueX='+x+'  ')

        }
    document.write("<br />");


}

}

function to3P(n){
return (n/100).toFixed(2).replace(/\./, '');
};
</SCRIPT>
</body>
</html>

What I'm trying to work out is how to LEAVE the form on the page after the document.write happens..(so the user doesn't have to keep pressing back to try new data!

A: 

Don't use document.write - if you use it after the page loads, you're essentially creating a new document. You can either use the javascript DOM functions or just a

<div id="results"></div>


<script> document.getElementById('results').innerHTML = "Your text here"; </script>
Sam Dufel
cheers mate. thank you
Matt Ayres
Might want to change the comma to a fullstop before innerHTML!
Alex
document.getElementById('results'),innerHTML should be document.getElementById('results').innerHTML (with period)
pferdefleisch
Ah yes... good catch, typo there.
Sam Dufel
haha wondered why it wasnt working! thanks all
Matt Ayres
A: 

document.write() is to blame. You should normally avoid it at all costs in favour of DOM manipulation and AJAX techniques. See here and here and particularly here and there's a metric ton of coverage on this subject around the web.

annakata
thanks ill check those out
Matt Ayres
A: 

Try to write in a div instead of the document, the form will still be visible

<input TYPE="button" NAME="button" Value="Go!" onClick="testResults(this.form)">
<div id="myResult/>

and in the javascript:

document.getElementById('myResult').innerHTML = 'your text here'
oyo
+1  A: 

It looks like you're using an example from 1995 to write your HTML... You should just read over some more modern html/javascript tutorials. Just a few good things to clean up:

  1. The language="javascript" attribute is depricated. You only need to specify <script type="text/javascript">
  2. It's better to use lowercase in your element declarations (ie: <p> instead of <P>)
  3. For every element that you open, you need to close. So for each <p>, you need a </p>. If there is no closing tag, ie, it is self-closing, as with the <br> tag, you should write it: <br />, just like you did in the document.write code. This makes your code truly XHTML compliant as you have specified in your doctype.
  4. As already stated, no reason to pass in the form to the javascript function. And just access the elements by id and their values via innerHTML.
Bal