[英]After include javascript file, how i can delete/remove
because I use ajax, to access content on my website; 因为我使用ajax来访问我网站上的内容; I have this code # 1 that dynamically includes a javascript file of different directory (depending on the content loaded) but that includes the same function.
我有这个代码#1动态包含不同目录的javascript文件(取决于加载的内容)但包含相同的功能。
Code #1: 代码#1:
var formContent = $("body form").first().attr('name');
if (typeof formContent != 'undefined') {
var uri = "classJS/" + formContent + "/jsc.js"; //File Javascript repeate in diferent directories.
fetch(uri, { method: 'GET', type: 'HEAD' }).then(function(resp) {
if (resp['status'] === 200) {
//if Exist the Default Function (FunctionForms) set this to undefined.
if (window.isFunction(window.FunctionForms)) {
window.FunctionForms = undefined;
}
$.getScript(uri).done(function() {
window.FunctionForms(formEvent); //call or Re-Call the function.
});
} else {
console.log('%cNot find Script file: ' + formEvent, 'color: red');
}
});
}
Code # 2, This function (repeated in every file) is dedicated to charge other specific function for the content loaded, example: 代码#2,此功能(在每个文件中重复)专用于为加载的内容收取其他特定功能,例如:
Code #2 File 1: (Can be loaded N times) 代码#2文件1 :(可以加载N次)
function FunctionForms(formEvent) {
console.log('%cCarga de Script JS para: ' + formEvent, 'background: #222; color: #bada55');
window.Test();
}
function Test(){
$('[name="s_list_2"]').unbind('click.form');
$(document).on("click.form", '[name="s_list_2"]', function(event) {
console.log('Is clicked');
});
}
this same file from other directories may have a subtly different content: 来自其他目录的同一文件可能有一个微妙的不同内容:
function FunctionForms(formEvent) {
//not used.........
console.log('%cCarga de Script JS para: ' + formEvent, 'background: #222; color: #bada55');
}
Then: it happens that if I enter the same content 5 times; 然后:如果我输入相同的内容5次,就会发生这种情况; the system includes 5 times the same file;
系统包含相同文件的5倍; but it does not overwrite the function or remove it, in the opposite case it stores it in different instances;
但它不会覆盖该函数或将其删除,相反,它会将其存储在不同的实例中; causing it to run N number of times: in a console ooutput I get this.
导致它运行N次:在控制台ooutput我得到这个。
VM6933:27 Is clicked
VM6944:27 Is clicked
VM6986:27 Is clicked
VM7022:27 Is clicked
VM7105:27 Is clicked
I understand that apparently this is not doing what I expect: 我明白这显然不是我所期望的:
if (window.isFunction(window.FunctionForms)) {
window.FunctionForms = undefined;
}
The problem is not with the function, the problem is with the event binding. 问题不在于功能,问题在于事件绑定。 Every time you load the file, it does:
每次加载文件时,它都会:
$(document).on("click.form", '[name="s_list_2"]', function(event) {
console.log('Is clicked');
});
which adds another click handler. 这会添加另一个点击处理程
It looks like you tried to remove the old handler first, but you didn't do it correctly. 看起来你试图先删除旧的处理程序,但是你没有正确地执行它。 To remove a delegated event handler, you have to use the same delegation syntax with
.off()
: 要删除委托事件处理程序,必须使用与
.off()
相同的委派语法:
$(document).off("click.form", '[name="s_list_2"]');
You're using: 你正在使用:
$('[name="s_list_2"]').unbind('click.form');
which would only remove a handler that had been added with: 这将只删除已添加的处理程序:
$('[name="s_list_2"]').on('click.form', function ...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.