
Apu
January 19, 2024 ›
#Javascript
›
#Q&A
❌
window onload is not a function
Hey everyone, I'm having a problem with the error message 'window.onload is not a function.' Here's my code:
<script>
window.onload = function() {
// Some code here
};
</script>
So, I'm trying to execute some code when the page loads, but I keep getting this error. Can someone please help me understand why this is happening and how to fix it?
save
listen
AI Answer
window onload is not a function
5
Hey everyone, I'm having a problem with the error message 'window.onload is not a…
asked
Apu
5 answers
2915
Hey everyone, I'm having a problem with the error message 'window.onload is not a…
Answer Link
answered
Apu
window.addEventListener('load', function() {
// Your code here
});
This code adds an event listener for the load event, which is a safer way to execute code when the page loads. It won't conflict with other scripts that might have set window.onload differently.
$(window).on('load', function() {
// Code with jQuery
});
But it's not working as expected. Any ideas on how to make this work with jQuery?
$(document).ready(function() {
// Your jQuery code here
});
This way, your jQuery code will execute only after the DOM is fully loaded, which is equivalent to window.onload.
https://www.3schools.in/p/embed.html?q=CjxzY3JpcHQ+CiB3aW5kb3cub25sb2FkID0gZnVuY3Rpb24oKSB7CiAgICBzZXRUaW1lb3V0KGZ1bmN0aW9uKCkgewogICAgICAgIGNvbnNvbGUubG9nKCJEZWxheWVkIGxvZyIpOwogICAgfSwgMTAwMCk7CiAgICAKICAgIGNvbnNvbGUubG9nKCJJbW1lZGlhdGUgbG9nIik7CiB9Owo8L3NjcmlwdD4=
The immediate log appears before the delayed one. How can I ensure the delayed code executes after the immediate one?
https://www.3schools.in/p/embed.html?q=CjxzY3JpcHQ+CiB3aW5kb3cub25sb2FkID0gZnVuY3Rpb24oKSB7CiAgICBjb25zb2xlLmxvZygiSW1tZWRpYXRlIGxvZyIpOwoKICAgIHNldFRpbWVvdXQoZnVuY3Rpb24oKSB7CiAgICAgICAgY29uc29sZS5sb2coIkRlbGF5ZWQgbG9nIik7CiAgICB9LCAxMDAwKTsKIH07Cjwvc2NyaXB0Pg==
By placing the console.log("Immediate log"); before the setTimeout, it ensures that the immediate log gets executed first, followed by the delayed log after the specified timeout.