SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    0 Post(s)
    0 Thread(s)

    quicky with document.write

    I'm pretty much a newb with javascript, I'm trying to do something pretty basic I think but stuck at the last part. Here is a link to the script so you can take a look:
    You enter a number in each box then click submit, it will do the functions and display the results. Though, the page gets stuck in a "loading" state after the results are posted and the form doesn't show up again. Not sure why it does that? I just wanted the messages to show up and the form to reload.

    <script type="text/javascript">
    	function validate_form(form) {
    		var number1 = form.number1.value;
    		var number12 = Math.sqrt(number1);
    		var number2 = form.number2.value;
    		var number22 = (number2 / 2);
    		document.write('Number1: '+number1+"<br />");
    		document.write('Number12:'+number12+"<br />");
    		document.write('Number2: '+number2+"<br />");
    		document.write('Number22:'+number22+"<br /><br />");
    		document.write('Thank you for using this program!');
    <form onsubmit="return validate_form(this);">
    	Number 1: <input name="number1" type="text" value=""><br />
    	Number 2: <input name="number2" type="text" value=""><br />
    			  <input type="submit" value="Submit">
    Need this finished asap, thanks for any help!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Christchurch, New Zealand
    104 Post(s)
    4 Thread(s)
    I suspect that you are using Firefox.

    The reload problem is occurring because the page is trying to submit the form to a new page.

    Return false from the function, which is then returned to the onsubmit event, to prevent the page from attempting to submit the form.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Sydney, NSW, Australia
    25 Post(s)
    1 Thread(s)
    Another issue is that the first document.write statement will also attempt to load a new page since the script is running after the page has finished loading and document.write can only be used before the page finishes loading. So your document.write statements will really need to generate an entire web page if you are going to use them that way.
    Stephen J Chapman, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts