简体   繁体   English

如何获取已加载文件的内容(带有脚本或链接)

[英]How can I get the content of a loaded file (with script or link)

I was wondering, how I get the content of a loaded script, stylesheet, ... bye an accessing an id set on the element. 我想知道如何获取加载的脚本,样式表的内容,...再访问元素上的ID集。

Example: 例:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Demo</title>
    <script src="../jquery-1.8.3.min.js"></script>
    <script id="test" src="test.txt" type="text/isis-template"></script>
    <!-- File contains "Hello world" -->
    <script>
        $(function () {
            $('#test').GET_CONTENT_OF_LOADED_FILE
        });
    </script>
</head>
<body>
</body>
</html>

Background: I want to load HTML templates (eg for mustach, knockout), but don't want to write them into the page, I'd rather have them in a seperate file. 背景:我想加载HTML模板(例如,用于胡子,敲除),但不想将它们写入页面,我希望将它们放在单独的文件中。 Now I saw that I can load any file with the script or link tag, so I was testing if I can load them like this.... Any comments why this might be a bad idea or how it can be done better are appreciated. 现在,我看到可以使用script或link标记加载任何文件,因此我正在测试是否可以像这样加载它们。...为什么这可能不是一个好主意或如何可以做得更好的任何评论。

Try using load() 尝试使用load()

$(function () {
      $('#test').load('yourfolder/test.html', function(resp){
           alert(resp); 
      });
});

If you have already load the contents in some html element 如果您已经在某些html元素中加载了内容

contents = $('#test').html();

If you use debug tools and see the network activity, you will see that it does not load the external files ( since it is not a text/javascript , and the browser does not know how to handle it ) 如果您使用调试工具并看到网络活动,您将看到它不会加载外部文件( 因为它不是text/javascript ,并且浏览器也不知道如何处理 )。
( wrong test on my part, was testing local files ) 我的测试错误,正在测试本地文件

So you only have a tag there with an id and an external resource in the src attribute. 因此,在src属性中只有一个带有id和外部资源的标签。 Treat it as just metadata. 将其视为元数据。

You will have to manually load the resources 您将必须手动加载资源

something like this 像这样的东西

// load external template resources
$('script[type="text/isis-template"]').each(function(){
    $(this).load(this.src);
});

For actual use you would need to make sure the templates are loaded before you try using them.. 对于实际使用,您需要先确保已加载模板,然后再尝试使用它们。

So you want the content of text/isis-template file when document is ready? 因此,当文档准备好后,您是否想要text / isis-template文件的内容? You are lucky because this file doesn't fall for CORS but mine(the answer i was looking for and came here today) do. 您很幸运,因为该文件不是CORS的,而是我的(我一直在寻找的答案,今天来到这里)。

Well just do ajax! 好吧,做ajax!

<!doctype html>
<html>
 <head>
   <meta charset="utf-8">
   <title>Demo</title>
   <script src="../jquery-1.8.3.min.js"></script>

   <!-- File contains "Hello world" -->
   <script>
     $(function () {
        $.ajax({ url: "test.html"})
         .done(function(cont) {
             var GET_CONTENT_OF_LOADED_FILE=cont;
         });
     });
   </script>
  </head>
  <body></body>
 </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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