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
|
1 Answer
Reset to default 3body 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
1 Answer
Reset to default 3body 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
版权声明:本文标题:javascript - How to trigger inline script onload? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656598a2161632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
p
element and also on DOM ready – Rory McCrossan Commented Nov 16, 2024 at 19:42