admin管理员组

文章数量:1377627

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>

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>
Share Improve this question asked Feb 22, 2009 at 16:53 RexERexE 17.7k16 gold badges62 silver badges85 bronze badges 1
  • When I clicked the "Call function" button, my Firefox tab shows a "page loading" icon indefinitely. – RexE Commented Feb 22, 2009 at 17:39
Add a ment  | 

6 Answers 6

Reset to default 3

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>

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

alert("Hello");

instead of the document.write.

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.

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 plete');
       }
     </script>
   </head>
   <body>
     <input type='button' onclick='myFunction();' value='Call Function'>
     <br>
     <div id='MyOutputDiv'></div>
   </body>
 </html>

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

<html xmlns="http://www.w3/1999/xhtml">
<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>

I can't explain the "hang" but please take a look at this page for a primer on document.write: http://javascript.about./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.

本文标签: javascriptNewbie hanging browser on function callStack Overflow