A
A
Alyosha2017-09-25 00:46:10
JavaScript
Alyosha, 2017-09-25 00:46:10

How to implement ajax loading of goods on pure js?

For example, the page is loaded first, and a request is made to the server, and objects are inserted into the wrapper:

document.getElementsByClassName('wrapper')[0].innerHTML = this.responseText;

At the next request, insert like this?
document.getElementsByClassName('wrapper')[0].innerHTML = document.getElementsByClassName('wrapper')[0].innerHTML +  this.responseText;

Is this good practice?
Is it possible to put html code at the end of the div more elegantly?
document.getElementsByClassName('wrapper')[0].innerHTML +  this.responseText;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RubaXa, 2017-09-25
@RubaXa

Slowly, very, so it will be faster:

const helper = document.createElement('div');
const wrapper = document.querySelector('.wrapper');

helper.innerHTML = this.responseText;
wrapper.appendChild(helper);

// Если нужно без лишнего div
let child;
let frag = document.createDocumentFragment()

while (child = helper.firstChild) {
   frag.appendChild(child);
}

wrapper.appendChild(frag);

P
Pavel Kornilov, 2017-09-25
@KorniloFF

Quite normal practice.
If there are a lot of inserts, you need to use documentFragment for optimization

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question