Posted By

arpo on 01/13/11


Tagged


Versions (?)

Client Side Include


 / Published in: JavaScript
 

From: http://www.boutell.com/newfaq/creating/include.html

  1. function clientSideInclude(id, url) {
  2.  
  3. //Usage
  4. //clientSideInclude('footer', '_includes/html/footer.html');
  5.  
  6. var req = false;
  7. // For Safari, Firefox, and other non-MS browsers
  8. if (window.XMLHttpRequest) {
  9. try {
  10. req = new XMLHttpRequest();
  11. } catch (e) {
  12. req = false;
  13. }
  14. } else if (window.ActiveXObject) {
  15. // For Internet Explorer on Windows
  16. try {
  17. req = new ActiveXObject("Msxml2.XMLHTTP");
  18. } catch (e) {
  19. try {
  20. req = new ActiveXObject("Microsoft.XMLHTTP");
  21. } catch (e) {
  22. req = false;
  23. }
  24. }
  25. }
  26. var element = document.getElementById(id);
  27. if (!element) {
  28. alert("Bad id " + id +
  29. "passed to clientSideInclude." +
  30. "You need a div or span element " +
  31. "with this id in your page.");
  32. return;
  33. }
  34. if (req) {
  35. // Synchronous request, wait till we have it all
  36. req.open('GET', url, false);
  37. req.send(null);
  38. element.innerHTML = req.responseText;
  39. } else {
  40. element.innerHTML =
  41. "Sorry, your browser does not support " +
  42. "XMLHTTPRequest objects. This page requires " +
  43. "Internet Explorer 5 or better for Windows, " +
  44. "or Firefox for any system, or Safari. Other " +
  45. "compatible browsers may also exist.";
  46. }
  47. }

Report this snippet  

You need to login to post a comment.