Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check body load (or other element)

Tags:

jquery

I want to show my div (wrapper) when body is loaded. This is my code:

$("div.wrapper").hide();
$('body').load(function() {
    $('.wrapper').show();
});

But my code not work. What is my mistake?

like image 339
Morteza Avatar asked Jan 12 '12 15:01

Morteza


People also ask

What is the difference between on load() and document ready()?

load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.

What is HTML onload?

Definition and Usage The onload attribute fires when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).


2 Answers

Description

.ready() Specify a function to execute when the DOM is fully loaded.

So make your div invisible using css and make it visible using jQuery.

Sample

Html

<div class="wrapper" style="display:none">
   <!-- your content -->
</div>

jQuery

$(document).ready(function() {
   $('.wrapper').show();
});

Complete Sample

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title></title>

        <script type="text/javascript" src="PathToYourJqueryJsFile" />
        <script type="text/javascript">
            $(document).ready(function () {
                $('.wrapper').show();
            });
        </script>
    </head>
    <body>
        <div class="wrapper" style="display:none">
        <!-- your content -->
        </div>  
    </body>
</html>

More Information

  • jQuery.ready()
  • jQuery.show()
like image 103
dknaack Avatar answered Oct 11 '22 02:10

dknaack


First, set .wrapper {display:none} in your css file. Then, this code should show it after the page has loaded:

$(document).ready(function() {
    $('.wrapper').show();
});
like image 43
blake305 Avatar answered Oct 11 '22 04:10

blake305