tags:

views:

342

answers:

6

I just started learning JavaScript and am wondering why this simple snippet hangs when I click on the "Call function" button. What am I missing?

<html>
<head>

<script type="text/javascript">
function myfunction()
{
document.write("hello");
}
</script>

</head>
<body>

<form>
<input type="button" 
onclick="myfunction()" 
value="Call function">
</form>

</body>
</html>
+1  A: 

Where do you expect the function to output its "hello"? Right into the button's source code? That makes no sense. The browser is confused and hangs.

Document.write doesn't magically insert something at the end of your document. It writes its stuff out right there where it is called.

RegDwight
You are incorrect. When a document is parsed document.write can be used to insert content. After the parsing the document is closed. If document.write is used after the document was closed, a new document is created and replaces the original document. The browser is not confused nor hangs.
some
The OP's browser obviously does hang, he clearly states that right there in his question.
RegDwight
+3  A: 

If you simply want to see your button doing something then try:

alert("Hello");

instead of the document.write.

Iain M Norman
+3  A: 

You need to write inside an element or give an element a value, or you should use document write like that :

<html>
<head>

<script type="text/javascript">
function myfunction()
{
document.getElementById("lblMessage").innerText = "hello";
document.getElementById("txtMessage").value = "hello";
//document.write("hello");
}
</script>

</head>
<body>

<form>

<script type="text/javascript">
document.write("This is written while page processed by browser !<br />");
</script>

<input type="text" id="txtMessage" /><br />
<span id="lblMessage"></span><br />
<input type="button" 
onclick="myfunction()" 
value="Call function">
</form>

</body>
</html>
Canavar
Great ScarletGarden! Thanks for showing me how to do it right. (Side note: I noticed that the innerText bit didn't work for me in Firefox on Safe Mode, although it worked in IE and Chrome.)
RexE
You're welcome, Javascript is an exciting scripting language, enjoy !
Canavar
+1  A: 

Not too sure what you mean by "hang"... Try this out... The alerts can be removed, but will inform you of where it is at in execution...

<html>
  <head>
     <script type="text/javascript">
       function myFunction() {
          //for debugging
          alert('Made it into function');
          document.getElementById('MyOutputDiv').innerHTML = 'Word To Your Mom...';
          //for debugging
          alert('function complete');
       }
     </script>
   </head>
   <body>
     <input type='button' onclick='myFunction();' value='Call Function'>
     <br>
     <div id='MyOutputDiv'></div>
   </body>
 </html>
RSolberg
A: 

document.write, but where? You have to specify this.

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>Hello</title>
</head>
<body>
<pre><script type="text/javascript">
function myfunction() {
  d = document.getElementById('hello');
  d.style.display = "block";
  d = document.getElementById('callme');
  d.style.display = "none";
}
</script>
</pre>
<div id="hello" style="display:none">
Hello
</div>
<div id="callme">
  <input type="button" 
     onclick="myfunction()" 
     value="Call function">
  </input>
</div>
</body>
</html>
Roman Glass
A: 

I can't explain the "hang" but please take a look at this page for a primer on document.write: http://javascript.about.com/library/blwrite.htm I would also like to second that you probably want to write to a specific element in the page rather than just overwriting the entire page.

Swingley