9

I have some html pages with the same footer. With javascript and only javascript could i import another html page inside it?

ulima69
  • 1,840
  • 5
  • 26
  • 43

6 Answers6

11

As above, one method is to use jQuery load. I happened to be doing the exact same thing now, so will post a quick example.

Using jQuery:

$("#yourDiv").load('readHtmlFromHere.html #readMe');

And your readHtmlFromHere.html page would consist of:

<div><div id="readMe"><p>I'm some text</p></div></div>
Ricky
  • 7,334
  • 2
  • 31
  • 44
7

Here's how you could use just javascript to add a footer to your page.

var ajax = new XMLHttpRequest();
ajax.open("GET", "footer.htm", false);
ajax.send();
document.body.innerHTML += ajax.responseText;
gilly3
  • 78,870
  • 23
  • 132
  • 169
3

You can use ajax to return a whole HTML page. If you wanted to replace the whole page you could replace the body tag and all it's children currently on the page with the body tag returned from the ajax call.

If you wanted to just replace a section you'd have to write a server-side script to create that section, then use ajax as above but just replace an element rather than the whole page.

Alex
  • 7,134
  • 1
  • 16
  • 31
2

Along with what @Alex mentioned, jQuery has a method .load() that you can use to fetch a specific portion of a page (See Loading Page Fragments heading on that page). You specify the URL you want to retrieve along with a selector (so if you wanted only a specific <DIV>'s contents for instance).

Brad Christie
  • 96,086
  • 15
  • 143
  • 191
0

You could do a server side include, depending on your webserver. but the quickest way would probably be to create a JavaScript file that uses document.write or similar to output the html syntax. and then just include the created JavaScipt file the normal way. more info at: http://webdesign.about.com/od/ssi/a/aa052002a.htm

Stephan
  • 1,265
  • 1
  • 10
  • 19
0

You definitely could, but if all you're doing is templating I recommend you do this on the server.

Andrew Noyes
  • 4,851
  • 1
  • 15
  • 13