admin管理员组

文章数量:1026989

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.

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.

本文标签: javascriptHow to trigger inline script onloadStack Overflow