Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loading div content with jQuery after rest of page loads

Posted on 2011-03-17
4
Medium Priority
?
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 2000 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:Gurvinder Pal Singh
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

704 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