简体   繁体   English

Ajax上的动态JavaScript文件请求创建冲突

[英]Dynamic JavaScript files on Ajax Request creating conflict

I am using MVC3 with heavy usage of ajax to get Partial Views. 我使用MVC3大量使用ajax来获取部分视图。 If Partial view contains JavaScript then it is added as a new js file as shown in snapshot: 如果部分视图包含JavaScript,则将其添加为新的js文件,如快照中所示:

ajax调用上的动态JS文件

so If I have a js function: 所以如果我有一个js函数:

function checkValue(){
   //do work
}

on ajax call a new dynamic JS file will be added contained this function and it conflicts with old once. 在ajax调用时,将添加包含此函数的新动态JS文件,它与旧的一次冲突。

myfile.js contained: myfile.js包含:

function checkValue(){
   //do work
}

and 1.js (dynamic file) will contain it too 和1.js(动态文件)也将包含它

function checkValue(){
   //do work
}

So when I call it due to presence in old file it call already present function which is outdated. 因此,当我因为存在于旧文件中而调用它时,它调用已经过时的已存在function How to solve this situation like new JavaScript replace old one. 如何解决这种情况,如新的JavaScript取代旧的。

Thanks 谢谢

You can check whether something is defined and redefine it only if it is not: 您可以检查是否定义了某些内容,并且只有在不是这样的情况下才重新定义它:

var checkValue = checkValue || function () {
     //do work
};

If you want your definitions to override each-other instead of defining the function with a name, define them on the global object each time: 如果希望定义覆盖彼此而不是使用名称定义函数,则每次在全局对象上定义它们:

window.checkValue = function () {
     //do work
};

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

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