最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to trigger inline script onload? - Stack Overflow

matteradmin7PV0评论

I have the following script, as seen, it calls the Date method onClick event, like so:

<!DOCTYPE html>
<html>

<body>

  <p onclick='this.innerHTML = Date();'>click me!</p>


</body>

</html>

I have the following script, as seen, it calls the Date method onClick event, like so:

<!DOCTYPE html>
<html>

<body>

  <p onclick='this.innerHTML = Date();'>click me!</p>


</body>

</html>

How can I do the same with an onLoad event? I don't want to define it with a separate <script> tag and call it as a function. I'd like the most minimal coding. Why doesn't the following work?

<!DOCTYPE html>
<html>

<body>

  <p onload='this.innerHTML = Date();'>date here onload</p>


</body>

</html>

Share Improve this question asked Nov 16, 2024 at 14:27 iuiu 3,1423 gold badges19 silver badges33 bronze badges 5
  • 2 <p> elements don't have an onload event. – Guy Incognito Commented Nov 16, 2024 at 14:41
  • @GuyIncognito So, how to circumvent that? – iu Commented Nov 16, 2024 at 14:43
  • 1 Give the element an id, attach a handler to the window's onload event, and change the element contents by referring to it by the id – Guy Incognito Commented Nov 16, 2024 at 14:46
  • 1 Does the script have to be inline? It's generally not a very good practice to do that. Put the logic in to a function, then call it on the click event handler of your p element and also on DOM ready – Rory McCrossan Commented Nov 16, 2024 at 19:42
  • @RoryMcCrossan after all the responses including yours, I say it doesn't have to be inline, and I agree with you. Thank you for your comment. – iu Commented Nov 16, 2024 at 20:26
Add a comment  | 

1 Answer 1

Reset to default 3

body tag of html has onload function.

The onload event occurs when an object has been loaded.

onload is most often used within the element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

The onload event can be used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

I am not sure if this is recommended.

Here, you can do like this.

<body onload="document.querySelector('#date-container').innerHTML = Date()">
  <p id="date-container"></p>
</body>

Hope, this helps.

Post a comment

comment list (0)

  1. No comments so far