load('header.html', function(html) {
document.getElementById('includedContent').innerHTML = html;
}, console.error);
function ajax(params) {
var request = new XMLHttpRequest();
request.open(params.method || 'GET', params.url || window.location.href, params.async || true);
request.setRequestHeader('Content-Type', params.contentType || 'application/x-www-form-urlencoded; charset=UTF-8');
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
if (params.success) params.success(this.response, this.statusText, this.status);
} else {
if (params.error) params.error(this);
}
}
};
request.send(params.data ? JSON.stringify(params.data) : '');
request = null;
}
function load(link, success, error) {
ajax({
url: link,
method: 'GET',
success: success,
error: error
});
}
// Возможные параметры функции ajax
// ajax({
// method: 'GET',
// url: 'https://text.com',
// async: true,
// contentType: 'application/json',
// data: { test: true },
// success: function(response, statusText, status) {},
// error: function(xhr)
// })