使用Javascript获取脚本文件的内容 [英] Getting content of a script file using Javascript

查看:83
本文介绍了使用Javascript获取脚本文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页中有以下脚本元素:

I have the following script element in my web page:

<script src="default.js" type="text/javascript"></script>

使用JavaScript,我想要检索脚本文件的内容。我知道我可以使用一个ajax请求来获取数据,但是我从本地已经拥有的服务器中得到一些东西。

Using JavaScript, I want to be able to retrieve the content of the script file. I know I could use a ajax request to get the data but then I am getting something from the server that I already have locally.

所以我更喜欢做的是从DOM中检索内容(如果可能的话)或具有相同结果的内容。

So what I would prefer to do is retrieve the content from the DOM (if that's possible) or something that has the same result.

Cheers
Anthony

Cheers Anthony

更新

我正在尝试简化问题可能是一个糟糕的想法,我以为这样会导致较少的问题。

I was trying to simplify the question, maybe a bad a idea, I thought this way would cause less questions.

我的实际情况如下,我实际上有

The real situation I have is as follows, I actually have

<script type="text/html" class="jq-ItemTemplate_Approval">
   ...
   html template that is going to be consumed by jQuery and jTemplate
   ...
</script>

现在这样做不错,但这意味着每次加载页面时,都必须将模板作为部分发送的主页的html。所以我的计划是执行以下操作:

Now this works fine but it means each time the page loads I have to send down the template as part of the html of the main page. So my plan was to do the following:

<script src="template.html" type="text/html"></script>

这意味着浏览器会缓存template.html的内容,我不必发送每次都下来但是要做到这一点,我需要从文件中获取内容。

This would mean that the browser would cache the content of template.html and I would not have to send it down each time. But to do this I need to be able to get the content from the file.

在这种情况下,据我所知,通过ajax请求内容不会帮助所有这些,因为它必须回到服务器来获取内容。

Also in this case as far as I know requesting the content via ajax isn't going to help all that much because it has to go back to the server to get the content anyway.

推荐答案

我想你想要的要做的是在template.js中分配一个变量。然后你可以使用这个变量来使用任何你想要的jquery。例如:

I think what you want to do is to assign a variable inside template.js. Then you have the variable available for use wherever you want in jquery. Something like:

var tpl = "<div> ... </div>"

这不是一个更简单的解决方案吗?我们在Ext JS中这样做。我认为这将在jQuery中为您工作。

Wouldn't this be a simpler solution to your problem? We do this in Ext JS. I think this will work for you in jQuery.

这篇关于使用Javascript获取脚本文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆