Getting content of a script file using Javascript

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

I have the following script element in my web page:


Using JavaScript,

8条回答
  •  温柔的废话
    2021-02-07 19:50

    If I understand you correctly, you don't want to use Ajax to load an html template text, but rather have it loaded with the rest of the page. If you control the server side, you can always include the template text in an invisible div tag that you then reference from Javascript:

    
    
    

    If you are just looking for to load the template so that you can have it cached, you can put the contents in a variable like this:

    
    

    or you can load it using ajax and store the template in a variable so it is accessible. It's pretty trivial in jquery:

    var template;
    $.get("template.html", function(data){
      template = data;
    });
    

提交回复
热议问题