You can use $(document).ready function after properly load the page you want to run JavaScript codes. It’s a jQuery code and it runs entire page. This code include $( window ).on( “load”, function() { … }).
// A $( document ).ready() block.
$(document).ready(function() {
console.log( "ready!" );
});
You can short this code and can use as below code. If you are not familiar with jQuery u can use this method.
// Shorthand for $( document ).ready()
$(function() {
console.log( "ready!" );
});
You can pass anonymous function to this function.
// Passing a named function instead of an anonymous function.
function readyFn( jQuery ) {
// Code to run when the document is ready.
}
$( document ).ready( readyFn );
// or:
$( window ).on( "load", readyFn );
Now below code shows you with all the codes together we discussed before. You can get complete idea about $( document ).ready() function in jQuery.
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( document ).ready(function() {
console.log( "document loaded" );
});
$( window ).on( "load", function() {
console.log( "window loaded" );
});
</script>
</head>
<body>
<iframe src="http://techcrunch.com"></iframe>
</body>
</html>
Very nice blog post. I absolutely appreciate this website. Thanks!
Aw, this was a really good post. Taking a few minutes and actual effort to make a really good article… but what can I say… I hesitate a lot and never manage to get anything done.