I have a simple page, just trying to attach a load event to the document. I am not sure which event handler to use in jQuery to do this with. I tried $()
and $(document).ready
and .load
but neither seem to run the code at the right time. Should I be using .on
, .live
? Or is there something I am doing wrong. Here is a sample:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
$(document).ready(
showWidth()
);
function showWidth() {
alert($('#textTitle').width());
}
</script>
</head>
<body>
<div class="page">
<div id="title">
<h1 id="textTitle">yo</h1>
</div>
</div>
</body>
</html>
When I run this the alert displays null
.
The load event occurs when a specified element has been loaded. This event works with elements associated with a URL (image, script, frame, iframe), and the window object. Depending on the browser, the load event may not trigger if the image is cached (Firefox and IE).
$("document"). ready(function() { setTimeout(function() { $("ul. galleria li:first-child img"). trigger('click'); },10); });
$( document ). ready() ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ). on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. // A $( document ).
The load event occurs when the document has been completely loaded, including dependent resources like JavaScript files, CSS files, and images. The <img> and <script> elements also support the load event. Use the addEventListener() method to register an onload event handler.
You forgot to write your code into function.
$(document).ready(function() {
showWidth();
});
Or just simply
$(function() {
showWidth();
});
Try this http://jsfiddle.net/vdCwE/
Also, are you sure you are including jQuery in your page? I didn't see it in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With