JavaScript文件的依赖 - 选择性加载资源文件和放大器; prevent重复文件、放大器、选择性、加载

2023-09-11 22:31:30 作者:’情亦茜て、

这可能更多的是哲学辩论的,但这里是我的:

This might be more of a philosophical debate, but here is what I have:

在两个控件,它们共享一个Javascript资源库调用Web服务。 它们后容易配合使用对方,但并非总是如此。 在该javacsript文件他们都引用不容易分开。 JavaScript文件不应该在应用程序添加到每个页面。

在理想情况下我会分裂的控件之间的web服务,让每个呼叫的目标只是为了他们所需要的功能一个js资源文件。

Ideally I would divide the webservice between the controls and have each call a js resource file targeted just to the functionally they need.

我不知道是否有一个明显的更好的方法,我很想念......

I'm wondering if there is an obvious better way that I'm missing...

在其最简单的形式I具有:

In its simplest form I have:

<html> 
<head>
</head>
<div>
    <h1>Control1</h1>
    <script type="text/javascript" src="/Webservice.asmx/js"></script>
</div>
<div>
    <h1>Other stuff</h1>
</div>
<div>
    <h1>Control2</h1>
    <script type="text/javascript" src="/Webservice.asmx/js"></script>
</div>
</body>
</html>

修改 我有jQuery的提供给我,如果这提供了一个有用的方法。

Edit I have jQuery available to me if this provides a helpful method.

使用ASP.NET

推荐答案

解决具有以下JS&放我的问题; jQuery的code

Solved my issue with the following JS & jQuery code

if (thisResourceLoaded === undefined) 
{ 
    $.getScript('/Webservice.asmx/js');
    var thisResourceLoaded = true; 
}