There are multiple approaches you can take to achieve this. One effective method is to utilize the onload
event, which is triggered only after all content on the page has been fully loaded. Considering that your website is already using jQuery, the examples provided below will incorporate jQuery.
Within your user controls, you can initially hide them by including a style
attribute in a surrounding tag for your control:
<div style="display: none">
<!-- Optionally, you could use "visibility: hidden"
instead of "display: none". This will maintain the
control's placeholder without displaying it physically to the user.
-->
<!-- Put your control content here -->
</div>
Then, within your control, you can include JavaScript code like the following (assuming jQuery is included at the beginning of your page, as it currently is). This code should be placed directly within your control.
<script type="text/javascript">
$(window).load(function() {
$("#" + <%= this.ClientID %>).css("display", "block");
// To use visibility instead, try the line below
//$("#" + <%= this.ClientID %>).css("visibility", "visible");
});
</script>
To elaborate on how this functions...
Upon the initial page load, the control remains hidden by default and is not rendered. jQuery listens for the load()
event of your page. Once that event is triggered, the control is displayed. This occurs only after the entire content has finished loading.
You can also hide any "loading..." <div />
in this load event as well.
Another alternative, depending on your requirements, involves structuring your page with 2 main divs: a "loading" div and a "content" div. The loading div is displayed by default with a generic loading message, while the content div is initially hidden (or concealed behind an overlay as shown in the example below). The onload
event removes the loading elements from the page, revealing the images.
The example below demonstrates displaying a loading message overlaying the entire page until the loading process is completed.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Loading Test</title>
<style type="text/css">
body {
margin: 0px;
padding: 0px;
overflow: hidden/* Prevent user from scrolling. */
} /* Scrolling is re-enabled on load by JavaScript */
.loadingBackground {
background: #000;
filter: alpha(opacity=70); /* internet explorer */
-khtml-opacity: 0.7; /* khtml, old safari */
-moz-opacity: 0.7; /* mozilla, netscape */
opacity: 0.7; /* fx, safari, opera */
height: 100%;
width: 100%;
position: absolute;
}
.loadingWrapper {
position: absolute;
top: 15%;
width: 100%;
}
.loading {
margin: 0 auto;
width: 300px;
text-align: center;
background: #ffffff;
border: 3px solid #000;
}
</style>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(window).load(function() {
$('.loadingBackground, loadingWrapper, .loading').fadeOut('normal');
$('body').css('overflow', 'auto');
});
</script>
</head>
<body>
<div class="loadingBackground"></div>
<div class="loadingWrapper">
<div class="loading">
Please Wait...<br />
<img src="http://www.ajaxload.info/cache/FF/FF/FF/00/00/00/30-1.gif" />
</div>
</div>
<!-- Large Images included to increase load time to show the loading effect -->
<img src="http://upload.wikimedia.org/wikipedia/commons/3/3c/KillaryHarbour.jpg"
style="height: 100%; width: 100%" />
<img src="http://upload.wikimedia.org/wikipedia/commons/6/6c/Ireland_-_Plains_of_South_Kildare.jpg"
style="height: 100%; width: 100%" />
</body>
</html>