You can add HTML content to an element using element.innerHTML = ...
.
You can get the template content by using a XMLHttpRequest
.
Have a look at this:
function getTemplate(url, success) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
// If the request doesn't have an error status code,
// the contents of the loaded URL (request.responseText) are sent
// to the callback function (success).
if (request.readyState === 4 && request.status === 200)
success(request.responseText);
}
request.open("GET", url, false);
request.send();
}
You can use the function like shown in the code below. It uses a callback function for the success
argument, which you have to specify.
getTemplate(url, function(responseText) {
document.write(responseText);
// or add the responseText to your div with: yourDiv.innerHTML += responseText;
});
Note that the request will mostly fail when you try to load content from a different origin (like a different file on your computer, or when you try to load site A's content from a script on site B).