Solved

Loading div content with jQuery after rest of page loads

Posted on 2011-03-17
4
417 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
Comment Utility
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
Comment Utility
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
Comment Utility
You can get a bunch  of loading image here http://www.4shared.com/file/219475792/87cd6f0f/loading.html
0
 

Author Comment

by:sonic1234
Comment Utility
Thanks very much dsmile
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now