0

I have a problem. It's AJAX load page, and script tags execution of that page.

.then(response => {

            let restr: string = response;



            restr = restr.replace(/(<head[^>]*)(?:[^])*?\/head>/ig, '')
                .replace(/(<(\/?)body([^>]*?)>)/g, '')
                .replace(/(<style[^>]*)(?:[^])*?\/style>/g, '')
                .replace(/(<(\/?)html([^>]*?)>)/g, '')
                .replace(/(<app-root[^>]*)(?:[^])*?\/app-root>/ig, '')
                .replace(/(<\?[\s\S]*?\?>)|(<!DOCTYPE\s+\w+\s+\[[\s\S]*?\]>)|(<!\w[\s\S]*?>)/g, '')
                .replace(/href\s*=\s*(?:")/ig, 'href="/#')
                .replace(/href\s*=\s*(?:')/ig, "href='/#");



            let scripts = restr.match(/\<scr[\s\S]*?ipt>/g);


            this.srcfield.nativeElement.innerHTML = '';


            scripts.forEach(value => {
                var element = document.createElement('script');

                var valstr = value.match(/"([^"]+)*"/g)[0]
                    .replace(/"|'([^"]+)*"|'/g, '')
                    .replace(/text\/javascript/g, '');

                element.src = valstr;

                this.srcfield.nativeElement.appendChild(element);
            })

            this.response = restr;


        })

How do you realize, response is answer from server, it's just HTML page provided by text. I make some operation on this page, and product it to current user page, without reloading. But some tag script doesn't load, because it execute don't by order. How can i load script tags to correctly

P.S The page which loaded, was generate by YII2 Framework

1 Answer 1

1

you can use callback

function addScript( src,callback) {
  var script = document.createElement( 'script' );
  script.setAttribute( 'src', src );
  script.onload=callback;
  document.body.appendChild( s );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.