Garmaine Staff asked 4 months ago

I have a problem making a router that loads html and js files according to hash. Loading html according to hash changes works well. However, if you connect the js controller after html request, [TypeError: Cannot read property 'insertAdjacentHTML' of null] occurs. I tried running XMLHttpRequest in sync but it didn't work. How do I know that the html file is rendered with XMLHttpRequest?

goToRoute(htmlName, controllers) {
    ((scope) => {
        let url = 'views/' + htmlName,
            xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState === 4 && this.status === 200) {
                scope.rootElem.innerHTML = this.responseText;
            }
        };
        xhttp.open('GET', url, false);
        xhttp.send();

        if (xhttp.status === 200) {
            controllers.map((v) => v.action());
        }
    })(this);
}