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!

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.