简体   繁体   中英

how to add text in local file with javascript

I would like to add the line <script src="test.js"></script> after the line <script src="original.js"></script> in an existing .html-file with JavaScript.

All files are local (not on a webserver) but I have no clue how to do this. It would be very nice if someone would push me into the right direction.

On the client, you can inject the <script> element into the DOM after the DOM has loaded like this:

(function(d, script) {
    script = d.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.onload = function(){
        // remote script has loaded
    };
    script.src = 'test.js';
    d.getElementsByTagName('head')[0].appendChild(script);
}(document));

However, file manipulation needs to be done server side with a language like PHP, Ruby, C#, etc.

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