why does document.write clear the page?

Joined
12 Feb 2006
Posts
17,652
Location
Surrey
got it so that the user clicks a button then inputs a load of information and then a load of ifs and elses decide on the outcome and if its a valid answer document.write will display some information, the annoying thing is that its clearing the whole page and replacing it with the information.

why is it doing this and not just displaying the
information in it's correct place and how do i stop it?
 
here it is, remember only first time i really gone into javascript so if it's an old thing to do sorry. Loving learning js though, some cool stuff.

Code:
<html>

<head>

<script type="text/javascript">



</script>




</head>

<body>

<script type="text/javascript">

var chest = "0" 

function chestcheck( )

{

chest=prompt("What Chest size are you in inchs? Ranges from 34-46")



  if (chest < 34 )

    {
      alert("The size you chose is incorrect, Please choose a size between 34 and 46 inchs.")
    }

      else if (chest >= 34 && chest <= 37)

        {
          document.write("Your chest size is Small. The size you chose is " + chest)
        }

          else if (chest >= 38 && chest <= 41)

            {
              document.write("Your chest size is Medium. The size you chose is " + chest)
            }

              else if (chest >= 42 && chest <= 46)

                {
                  document.write("Your chest size is Large. The size you chose is " + chest)
                }

                  else if (chest > 46)

                   {
                      alert("The size you chose is too big. Please restart and choose a size between 34 and 46 inchs")
                    }
                    
}

</script>


<input type="button" value="Chest Check"
onclick="chestcheck()" >


<p>

hello world

</p>


</body>

</html>
as you can see it clears the hello world. I though it was because the script is in the head section but that didn't make a difference anyway. The only thing i can think of though is because the document.write has no where to produce the results except after the button but i can't see how you'd do it any differently?

thanks

also while its here why does the page continue to load something after you have run the script and then you can't refresjh the page either?
 
Last edited:
thanks that worked just write. few questions though if you could explain would be great.

so getElementById is that like the write part of document.write? does it need to be caps lock on all words except the first? what similiar things are there like this? like for instance is there getElementByDate or does it not work like that?

aswell with the innerHTML and writestr im not 100% sure what these meanif a beter explaination could be given? i see the results but not sure what its doing to get the results if that makes sense and would you'd call these things.

think thats it. thanks for the help folks. Like i say i see whats going on but i am only seeing and not fully understanding.

[edit]
oh and also why the return false? i remember reading about doing that in c++ aswell but forget why? im going to go read a load more tutorials i think
 
Last edited:
Back
Top Bottom