简体   繁体   中英

Load Javascript on runtime created Bootstrap Modal

I have this view where I call a Modal that loads from a external html file. Since the content of this Modal is generated in runtime, I have to load/reload my javascript files, right?

I've created a function that reloads my javascript files after this Modal is loaded and it works fine.

My Question: Is there any better way to accomplish this?

Have you tried to load a JS file when the external html file is included?

In the view:

<script>LoadJsAsync('/Scripts/modal-scriptName.js');</script>

In a JS file that is referenced:

function LoadJsAsync(jsPath) {
    var head = document.getElementsByTagName('body')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.onreadystatechange = function () {
        // if (this.readyState == 'complete') { callFunctionFromScript(); }
    }
    script.src = jsPath;
    head.appendChild(script);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM