Question

Is it possible to add all the body elements through a javascript/jquery function which has to be called before the page load (onload) ??

Eg:

<html><head>
<script type="text/javascript">
function LoadBodyHtml()
{
var bodyHtml = "<div>Sample Div<br>";
bodyHtml += "<table><tbody><tr><td>column1</td><td>column2</td></tr></tbody></table>";
bodyHtml += "</div>";
$(document.body).html(bodyHtml);
}
</script>
</head>
<body onload="LoadBodyHtml();"> </body>
</html>

No correct solution

OTHER TIPS

you should use $("body") and not $(document.body):

$("body").html(bodyHtml);

Fiddle

-->$().ready() function ll get executed during page load.

-->$("").html(CONTENT); to add elements in a Div..!

jquery Code:

 <script>
 $(document).ready(function() {
 $( "body" ).html("body elements");
 }
 </script>

Refer:

https://api.jquery.com/html/

http://api.jquery.com/ready/

You can try this:

<script type="text/javascript">
   (function LoadBodyHtml() {
      var bodyHtml = "<div>Sample Div<br>";
          bodyHtml += "<table><tbody><tr><td>column1</td><td>column2</td></tr></tbody></table>";
          bodyHtml += "</div>";
      $(document.body).html(bodyHtml);
    })();
</script>

and remove this onload="LoadBodyHtml(); inline event from body tag.

Demo fiddle

What is an Immediately-Invoked Function Expression?
It’s a function expression that gets invoked immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top