Getting content of a script file using Javascript

前端 未结 8 1194
谎友^
谎友^ 2021-02-07 19:38

I have the following script element in my web page:


Using JavaScript,

8条回答
  •  抹茶落季
    2021-02-07 19:50

    The way that most JavaScript import files work is they include a script, that immediately calls a function with a parameter of certain text, or of another function. To better illustrate, say you have your main index.html file, set it up like this:

    
    
    
    
    
    
    
    
    
    
    
    

    now make that other file, someOtherFile.js, and right away when its loaded, simply call that "started" function which should already be declared in the scope, and when thats done, then whatever text is passed, from the file, is stored in the main index.html file. You can even stringify an entire function and put it in, for example:

    started("superModule", (function() {
        /*
        
        {something}Entire Javascript / html template file goes here!!{/something}
        
    ?> */ }).toString());

    now you can access the inner content of the function, and get all the text in between the comments, or better yet, then do other parsing etc, or make some other kind of parsing identifiers at the beginning and end of the comments, as shown above, and get all text in between those

提交回复
热议问题