Solved

Loading div content with jQuery after rest of page loads

Posted on 2011-03-17
4
418 Views
Last Modified: 2012-08-13
I have an HTML page that includes a portion of code that fetched external data remotely.

eg.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
</head>

<body>
<h1>Hello</h1>
	<div id="weatherwidget">
		<!-- Widget Start -->
		<link rel="stylesheet" type="text/css" href="http://www.externalsite.com/style.css" />
		<script type="text/javascript" src="http://www.externalsite.com/javascript.js"></script>
		<div id="ww-widget">
			<p>Widget does stuff here when it finally loads.</p>
		</div>
		<!-- Widget End -->
	</div>
<h2>More text</h2>
</body>
</html>

Open in new window


The problem I am having is this external site is sometimes slow to load, and therefore makes the page loading pause as the external widget loads.

Is there a way in jQuery I can use AJAX to load that div once the rest of the page has loaded?


0
Comment
Question by:sonic1234
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 35162514
You should modify your html as attached, and create another html with this content and save it with name and path as specified in /path/to/widget/htm

				<link rel="stylesheet" type="text/css" href="http://www.externalsite.com/style.css" />
                <script type="text/javascript" src="http://www.externalsite.com/javascript.js"></script>

Open in new window

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>

<body>

<h1>Hello</h1>
        <div id="weatherwidget">
                <!-- Widget Start -->                
                <div id="ww-widget">
                        <p>Widget does stuff here when it finally loads.</p>
                </div>
                <!-- Widget End -->
        </div>
<h2>More text</h2>
<script language="JavaScript">
<!--
$(document).ready(function () {
  example_ajax_request();
});

	function example_ajax_request() {
  $('#weatherwidget').html('<p><img src="/images/ajax-loader.gif" width="220" height="19" /></p>');
  $('#weatherwidget').load("/path/to/widget/htm");
}

//-->
</script>
 </body>
</html>

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35162515
you can load the div content at the document.ready event

$(document).ready(function(){
   //load the div now
});
0
 
LVL 13

Expert Comment

by:dsmile
ID: 35162522
You can get a bunch  of loading image here http://www.4shared.com/file/219475792/87cd6f0f/loading.html
0
 

Author Comment

by:sonic1234
ID: 35186146
Thanks very much dsmile
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now