Jump to content

JavaScript function similar to PHP's include()?


melting_dog

Recommended Posts

Hi all,

 

I need to make a site that is entirely client side. I was wondering if any one new if there was a JavaScript equivilant of PHP's include()?

 

IE I want to include my header.html file at the top of every html page I make, rather then writing the same stuff again and again.

 

Any help would be great! Thanks!

You can do it a few different ways, but the easiest way is to just write a new script tag to the head. In regular javascript, you can do something like this:

 

var scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.src = "path/to/script.js";
var headTag = document.getElementsByTagName("head")[0];
headTag.appendChild(scriptTag);

 

Personally, I prefer using jQuery, which has the getScript function which loads the script into memory (making it usable):

$.getScript("path/to/script.js");

On the contrary, loading scripts after page load, using javascript makes for a faster page load, not a slower one, as it allows the scripts to load asynchronously.

OP doesn't want to load JS asynchronously.... he wants to load the HEADER AND FOOTER asynchronously... e.g. the TEMPLATE.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.