onlycode.org

javascript
jquery
html
angularjs
css
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
how to load scripts on a...

How to load Scripts on AJAX requests, just only code?

flask | ajax | html | javascript

edit
const loadHtml = (text, dest) => {
    const p = new DOMParser();
    const doc = p.parseFromString(text, 'text/html');
    const frag = document.createDocumentFragment();
    while (doc.body.firstChild) {
        frag.appendChild(doc.body.firstChild);
    }
    const ret = Promise.all([].map.call(frag.querySelectorAll('script'), script =>
        new Promise(res => {
            const scriptParent = script.parentNode || frag;
            const newScript = document.createElement('script');
            if (script.src) {
                newScript.addEventListener('load', e => {
                    res({src:script.src, loaded: true});
                });
                newScript.addEventListener('error', e => {
                    res({src:script.src, loaded:false});
                });
                newScript.src = script.src;
            } else {
                newScript.textContent = script.textContent;
                res({src:false, loaded:true});
            }
            scriptParent.replaceChild(newScript, script);
        })
    ));
    dest = document.querySelector(dest);
    if (replace) {
        dest.innerHTML = '';
    }
    dest.appendChild(frag);
    return ret;
};
let yourHTMLtext = '....'; // this would be the result of your AJAX call
loadHTML(yourHTMLtext, '#ajaxContent').then(results => {
    // at this point, all scripts have been loaded, if that's something you need to know
});

25
Jaromanda X, 27.09.2018 06:05

Add your just only code

your name:    



If you need full text click here

Some similar links How to load Scripts on AJAX requests? ↓:
How to differentiate Ajax requests from normal Http requests?

load scripts asynchronously

does not load scripts within ng-view

Load scripts after page has loaded?



This project is created for those who just need only code, wihout maaaaaaaaaaany words.