Solved

Loading div content with jQuery after rest of page loads

Posted on 2011-03-17
4
422 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
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…

856 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