Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Load external file into div (See related posts)

// javascript to go into head of document or external .js file

function ahah(url, target) {
  document.getElementById(target).innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};"GET", url, true);

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;

function load(name, div) {
        return false;

// call code with the name of the external file and the id of the div you want to put the content into

<a href="file1.html" onclick="load('file1.html','content');return false;">File 1a>

Comments on this post

gemelli2 posts on Jan 29, 2008 at 23:08
hi - i've just tried this snippet and have gotten the page to load into a div but when it loads, it seems like it's losing its styling and defaulting to the current page css. also it is loading at the bottom of the div. can anyone out there help me? i'm at a loss!! i'm using spry elements on the page i'd like to load (sliding panels) and a spry menu on the main page

here's the page:

--->on the left menu the "what we've accomplished" link is the one in question.
--->you can see the page that i'd like loaded and what its supposed to look like by clicking on the "recent news" link

thanks in advance for any help at all!!! i need it.

Spencer posts on Feb 05, 2008 at 17:51
Hey Gemelli,
When you pull in the page it's inheriting the styles of that page, so you need to include style data in the template, not the content. If you don't want to do that I guess you could do iFrames but I vote no. ;)
spencer dot dennis dot hill at gmail dot com in case you have more questions!

You need to create an account or log in to post comments to this site.

Related Posts