2

I was looking at another question and saw that they had been told, when their Javascript was not working, to put a function that started on loading of the page.

I was trying to use this code:(well, not exactly this code, but something like it)

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
function doThisWhenLoaded(){
    var code = document.getElementsByTagName("body");
    code.append("<p>Yes it did.</p>");
}
        </script>
    </head>
    <body onload="doThisWhenLoaded">
        <h1 style="font-family:sans-serif;">Did it work?</h1>
    </body>
</html>

And nothing happened. All that it did was show the h1 code that I put in there.

3
  • Possible duplicate of JavaScript that executes after page load Commented Nov 27, 2018 at 18:03
  • 2
    Also, you're using jQuery's append() incorrectly. It must be applied to a jQuery object ($('body').append()). Commented Nov 27, 2018 at 18:05
  • Ok, I will try that, because I just noticed that I was using JQuery on an element selected by Javascript. Commented Nov 28, 2018 at 13:44

3 Answers 3

2

Since you are already including jQuery try this

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
$(document).ready(function(){
    var code = $("body");
    code.append("<p>Yes it did.</p>");
});
        </script>
    </head>
    <body >
        <h1 style="font-family:sans-serif;">Did it work?</h1>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

2

You forgot to include the parenthesis to call the function in the body element's onload attribute:

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript" charset="utf-8">
    function doThisWhenLoaded() {
      //Modified the two lines below slightly
      var code = document.querySelector("body");
      code.innerHTML += "<p>Yes it did.</p>";
    }
  </script>
</head>

<body onload="doThisWhenLoaded()">
  <h1 style="font-family:sans-serif;">Did it work?</h1>
</body>

</html>

A better/cleaner way to achieve the same thing would be to use addEventListener and DOMContentLoaded event. Something like below:

document.addEventListener("DOMContentLoaded", appendHtml.call(null, {
  selector: 'body',
  html: '<p>Yes it did.</p>'
}));

function appendHtml(config) {
  document.querySelector(config.selector).innerHTML += config.html;
}
<h1 style="font-family:sans-serif;">Did it work?</h1>

1 Comment

I actually was trying to use JQuery, not just Javascript. The only reason this question's title contains the word Javascript is because I was confused and trying to use JQuery on a element selected by Javascript and it didn't work.
1

Two reason why it doesn't work:

  • To select the body you have to use document.body or $("body") (in jQuery)
  • And you forgot to add parentheses to your function call doThisWhenLoaded()

Here is the code corrected:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
function doThisWhenLoaded(){
    $("body").append("<p>Yes it did.</p>");
}
        </script>
    </head>
    <body onload="doThisWhenLoaded()">
        <h1 style="font-family:sans-serif;">Did it work?</h1>
    </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.