Dynamic Loading of External HTML file Using Jquery

 

I’ve been playing around with the Jquery framework lately. I’m not a big JavaScript guy as my primary focus for the past few years now has been ActionScript/Flash/Flex. But I have to admit, Jquery (and I’m sure some of the other more robust JavaScript frameworks) is really nice.

Anyways, here’s how to load in an external html file into a div:

<script type=”text/javascript” src=”jquery-1.2.6.js”>
</script>
<script type=”text/javascript”>
$(document).ready(function(){
$(function () {
$(”#content”).load(”content.html”);
});
});
</script>

Here is the content.html file:

<html>
<head>
</head>
<body>
<h1>Here is my text file!</h1>
<p>Here is the second line of it!</p>
<p><strong>And the third!</strong></p>
</body>
</html>

View the result here.

Simple content management? Or is that an SEO no-no? I’m used to loading, parsing, and displaying XML via Flash. I’ll have to look into whether Jquery can do that too.

This entry was posted in HTML, Jquery and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

 

7 Comments

  1. Posted October 15, 2008 at 12:11 pm | Permalink

    Easy and clean. Wouldn’t use to include any actual content that needs to be accessible and indexed by search engines but good for applications and interactive prototypes. Also ads could be inserted into the page after it has loaded to avoid troubles with slow ad-hosting services.

  2. Posted September 23, 2009 at 5:59 pm | Permalink

    Hello, really great post i appreciate you work!

  3. drago
    Posted October 14, 2009 at 9:58 am | Permalink

    Strange. it doesnt work

  4. Posted October 14, 2009 at 10:37 am | Permalink

    drago – it does work. Click on the example. Unless you’re using some wacked browser….

  5. drago
    Posted October 19, 2009 at 3:46 pm | Permalink

    I’ve tried to launch google.com and it not working.

    $(document).ready(function(){
    $(function () {
    $(”#content”).load(”http://mininova.org”);
    });
    });

    <div id="content" style="width:500px; height:200px"

    Probabli i dont understand what is the porpose of this code.

  6. ray
    Posted November 9, 2009 at 9:56 am | Permalink

    Brilliant and simple!

    Question: (from a non-techie, non-JQuery guy)
    How to keep any HTML code which exists in the DIV,
    before the jQ loads the “content.html” file ?

    example:
    In the demo page, the DIV has this text:
    Content:
    inside the DIV. See the source code…

    But…after the jQ loads the “content.html” file,
    the “Content: ” text is not visible anymore!

    So, what should the jQ code to “append” any new HTML content
    to the DIV’s already existing text?
    (instead of just substituting it completely…)

    Thanks!!
    ray

  7. Posted January 20, 2010 at 2:33 pm | Permalink

    Drago, you are violating same origin policy by loading up an external domain inside your javascript…Just search for Same Origin Policy in google.

One Trackback

  1. By jQuery « Thoughts on… on December 17, 2008 at 1:58 am

    [...] http://elearninglive.com/wordpress/2008/09/dynamic-loading-of-external-html-file-using-jquery/ Possibly related posts: (automatically generated)BoinxTV 1.0 released today [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Subscribe without commenting