jQuery: $(document).ready() is much better than onload()

--

It is very important to load Javascript functions only after the HTML (Hypertext Markup Language) or more precisely the DOM (Document Object Model) is completely loaded.

For this purpose there is the function onload in HTML, which can be executed in the <body> — tag.

<html>
<head>
<script type="text/javascript">
function load()
{
alert(„The page was loaded");
}
</script>
</head>
<body onload="load()">
<h1>page content</h1>
</body>
</html>

W3C says that all leading web browsers fully support it. In practice, however, it has been found that when working with jQuery it is much better and more reliable to use $(document).ready(function().

<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert(„The page was loaded");
});
</script>
</head>
<body>
<h1>page content</h1>
</body>
</html>

Javascript function if body onload is not used.

window.onload = function(){
alert(„The page was loaded");
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Never Code Alone
Never Code Alone

Written by Never Code Alone

PHP Training und Automatisiertes Website Testing

No responses yet

Write a response