简体   繁体   English

管理JavaScript包含文件

[英]Manage javascript include files

Currently we have an extensive web-based office application written in PHP, javascript to manage various processes (billing, ordering, user management, reporting, subscription management, article managementm, warehousing etc.) 当前,我们有一个使用PHP编写的,基于Web的广泛的办公应用程序,使用javascript来管理各种流程(开票,订购,用户管理,报告,订阅管理,商品管理,仓储等)。

The application uses iframes to load pages that perform various functions within the main application interface. 该应用程序使用iframe加载在主应用程序界面内执行各种功能的页面。 From the main page tabs are created so you can access all open pages if needed. 从主页上创建选项卡,以便您可以根据需要访问所有打开的页面。 Each page has their own javascript include-files, css etc to perform complex tasks. 每个页面都有自己的javascript包含文件,css等,以执行复杂的任务。 All iframed-pages (around 600) are on the same domain/host. 所有iframed页(约600个)都在同一域/主机上。

Since many people describe using iframes as 'evil', I was examining on how I could convert the application into using divs only. 由于许多人将使用iframe描述为“邪恶”,因此我正在研究如何将应用程序转换为仅使用div。

The quesions I have are: 我的问题是:

1) in a situation where there are no iframes anymore, do I need to include every used javascript files on the main start page? 1)在没有iframe的情况下,我是否需要在首页上包括所有使用过的javascript文件? Or is it possible to dynamically load javascript files into the memory depending on which page you access? 还是可以根据您访问的页面将javascript文件动态加载到内存中? (inluding new function declarations etc.) (包括新的函数声明等)

2) Can these include-files be removed completely out of the memory when someone closes a page? 2)有人关闭页面时,可以将这些包含文件完全从内存中删除吗? (in this case a div). (在这种情况下为div)。 This to avoid possible conflicts between function names & variables of different files and also not to burden the browser's memory usage. 这样可以避免不同文件的函数名和变量之间可能发生冲突,也不会给浏览器的内存使用造成负担。

3) Or perhaps iframes are not that 'evil' afterall, and this is a correct way for using iframes? 3)也许iframe毕竟不是那种“邪恶”的东西,这是使用iframe的正确方法吗?

thanks for the advise 谢谢你的建议

chees Patrick Chees Patrick

have you tried any code or just asking? 您是否尝试过任何代码或只是问?

you can use jquery ( see here ) 您可以使用jQuery( 请参阅此处

$.getScript("ajax/test.js", function(data, textStatus, jqxhr) {
   console.log(data); //data returned
   console.log(textStatus); //success
   console.log(jqxhr.status); //200
   console.log('Load was performed.');
});

As long as your javascript is modular, you can unload it whenever you want. 只要您的JavaScript是模块化的,就可以随时卸载它。

In case you don't know, in Javascript functions are objects. 如果您不知道,在Javascript函数中就是对象。 And with objects you can do this: 使用对象,您可以执行以下操作:

var a = {b:10, c:20, d:30}; //Some memory is allocated for object a
a = null; //Object a will be deleted, and no longer need any memory

How can you use that to handle functions? 您如何使用它来处理功能? Here's easiest explanation: 这是最简单的解释:

var myFunction = function(a,b,c) {
    alert(a+b+c);
}; //Here we have a function stored in memory
myFunction = null; //And now we don't

You can make whole libraries of functions this way and then remove them with one operation. 您可以用这种方法制作整个函数库,然后通过一次操作将其删除。 Example: 例:

var MyMath = {};
MyMath.abs = function(a) {
    return (a<0)?-a:a;
};
MyMath.min = function(a,b) {
    return a>b?b:a;
}; //MyMath uses some memory.
alert(MyMath.min(5,10));
MyMath = null; //Now it does not

So, wrap all of your libraries into objects, and unload those objects whenever you need. 因此,将所有库包装到对象中,并在需要时卸载这些对象。

Added: 添加:

When your browser sees some new Javascript code - it executes it all at once. 当您的浏览器看到一些新的Javascript代码时,它将立即执行所有这些代码。 It also does save it in memory, but never uses it again (basically it is stored the same way hello is stored in <div>hello</div> ). 它也确实将其保存在内存中,但是再也不会使用(基本上,它的存储方式与hello<div>hello</div>存储方式相同)。 Therefore the best solution would be: 因此,最好的解决方案是:

1) Add <script> tags dynamically to load desired Javascript files on demand. 1)动态添加<script>标记以按需加载所需的Javascript文件。 It will look something like: 它看起来像:

if (MyMathLibrary == null || MyMathLibrary == undefined) {
    createScriptElementForMathLibrary();
}

alert(MyMathLibrary.calculateEnthropyOfTheUniverse());

2) Whenever you no longer need the library, you just do MyMathLibrary = null . 2)每当您不再需要该库时,只需执行MyMathLibrary = null You can also delete it from DOM at this point, it won't do any harm, as I said, it is no more than invisible div at this point. 您也可以在此时将其从DOM中删除,它不会造成任何危害,正如我所说,这仅是不可见的div。

But honestly, are you sure this is worth the bother? 但老实说,您确定这值得打扰吗? People nowadays write 3D shooters purely in Javascript and computers come with 4-16 GB of memory. 如今,人们仅使用Javascript编写3D射击游戏 ,而计算机则具有4-16 GB的内存。 Also, it's common practice to first make a working program and bother with optimizations only if it lags. 另外,通常的做法是先编写一个工作程序,然后仅在滞后时才进行优化。 Otherwise it is likely that you will spend a month of work for something no user will ever notice. 否则,您可能会花一个月的时间来做一些用户不会注意到的事情。

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

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