简体   繁体   中英

Returning from JavaScript function to HTML Body

I am calling a JavaScript function from the HTML Body using the onload event. The JavaScript function executes successfully but the HTML Body contents are not displayed.

I believe, there is an issue with returning from the JavaScript to the HTML Body.

Here is how the code looks like:

<html>
<script type="text/javascript">
function display()
{
 document.write("You just executed JavaScript code");
 return true;
}
</script>

<body onload="display();">
<p>We are in HTML now</p>
</body>
</html>

This will display the text, "You just executed JavaScript code" in the browser. But the innerHTML of tags is not displayed.

I modified the onload event in tag as:

<body onload="return display();">

And, even this executes only the JavaScript.

If you just want to show the message as alert try this.

 <script type="text/javascript">
 function display()
 {
 alert("You just executed JavaScript code");
 return true;
 }
</script>
</head>
 <html>
 <body onload="display();">
 <p>We are in HTML now</p>
</body>

else

 <script type="text/javascript">
 window.onload=function()
 {
document.getElementById("js").innerHTML = "You just executed JavaScript code";
return true;
}
</script>
<body>
<p id="js"></p>
<p>We are in HTML now</p>
</body>
</html>

As per https://developer.mozilla.org/en/document.write

Once the document has finished loading, calling document.write() will actually first call document.open() , which replaces the currently loaded document with a new Document object. So what you're doing with your code is replacing the original document with one that only contains the string 'You just executed javascript code'.

So if you want to use document.write to place text inline, you would have to use it like so:

<html>
<body>
  <p>We are in HTML now</p>
  <script>
    document.write('You just executed javascript code');
  </script>
</body>
</html>

If you want to insert text into the document after it has finished loading, you'll need to use another method, like innerHTML .

Hi Neon Flash, I have done some work on your problem.I also research about where is the problem then i found some interesting points hope this will help you Check here

or you can use

Usually, instead of doing

document.write
someElement.innerHTML
document.createElement with an someElement.appendChild.

You can also consider using a library like jQuery and using the modification functions in there: http://api.jquery.com/category/manipulation/

Document.write is replacing the contents inside your body tag.

Try something like this.

<html>
<script type="text/javascript">
function display()
{
 document.getElementById("text-from-js").innerHTML = "You just executed JavaScript code";
 return true;
}
</script>

<body onload="display();">
<p>We are in HTML now</p>
<p id="text-from-js"></p>
</body>
</html>

As the previous answers said. document.write cannot be used for your purpose. And I strongly recommend that you don't use it anywhere. Its a bad practice.

For your purpose prepending/appending to document.body.innerHTML

ex: document.body.innerHTML += 'You just executed javascript code';

or something like

document.body.appendChild(document.createTextNode('You just executed javascript code'))

should do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM