SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    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:
    http://berryequipment.net/berry3/javascript.html
    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.

    Code:
    <html><head>
    <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!');
    
    	}
    </script>
    </head><body>
    <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">
    </form>
    </body></html>
    Need this finished asap, thanks for any help!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    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
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    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

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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