[英]Iframe load callback is called multiple times
I Have a simple web page and I have a following script. 我有一个简单的网页,我有一个以下的脚本。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function sendTraffic(typeOfTraffic){
var ipAddr = document.getElementById("serverIpaddr").value;
//alert(ipAddr);
$("#WAFTest").attr("src", '/cgi-bin/test.py?wat='+typeOfTraffic+'&ipaddr='+ipAddr);
$("#WAFTest").load(function () {
alert("Test done");
});
}
</script>
I have a button and this function sendTraffic is called. 我有一个按钮,这个函数调用sendTraffic。 What I am seeing is Test done alert comes up once for the first time , twice for the second time and thrice for the third time and it keeps going on.
我所看到的是Test done警报第一次出现一次,第二次出现两次,第三次出现三次并继续进行。
<button type="button" id="btnSendPing" class="btn btn-primary btn-lg" onclick="sendTraffic('sendping')">Send Ping Traffic</button>
I am not able to figure out what wrong I am doing. 我无法弄清楚我在做什么错。 I am using twitter bootstrap just FYI.
我正在使用twitter bootstrap,仅供参考。
EDIT: I am using chrome on Mac. 编辑:我在Mac上使用chrome。 #WAFTest is an Iframe.
#WAFTest是一个iframe。 I am just trying to get data from the server and load it to the Iframe.
我只是想从服务器获取数据并将其加载到Iframe。
Like the others said, your .load()
event should have been registered only once . 像其他人说的那样,你的
.load()
事件应该只被注册一次 。
If solution posted in other answers doesn't work for you (although it should), there's another way to go 如果在其他答案中发布的解决方案对您不起作用(尽管它应该),那么还有另一种方法
Attach a handler to an event for the elements.
将处理程序附加到元素的事件。 The handler is executed at most once per element per event type
每个事件类型的每个元素最多执行一次处理程序
function sendTraffic(typeOfTraffic){
var ipAddr = document.getElementById("serverIpaddr").value;
//alert(ipAddr);
$("#WAFTest").attr("src", '/cgi-bin/test.py?wat='+typeOfTraffic+'&ipaddr='+ipAddr);
$("#WAFTest").one('load', function () {
alert("Test done");
});
}
Althought solution posted in other answers is more proper. 在其他答案中张贴的解决方案更合适。 I would however remove
onclick
attribute and set click event handler for the element using jQuery. 然而,我会删除
onclick
属性并使用jQuery为元素设置click事件处理程序。
To avoid ambiguities (as .load()
is also a method used for AJAX request), I'd recommend use .on()
to bind the load
event handler 为了避免歧义(因为
.load()
也是用于AJAX请求的方法),我建议使用.on()
绑定load
事件处理程序
<iframe id="WAFTest"></iframe>
<button type="button" data-ping="sendping" class="btnSendPing btn btn-primary btn-lg">Send Ping Traffic</button>
<input id="serverIpaddr" value="something" />
$(document).ready(function(){
$('.btnSendPing').click(function(){
var ipAddr = $("#serverIpaddr").val();
var typeOfTraffic = $(this).data('ping');
$("#WAFTest").attr("src", '/cgi-bin/test.py?wat='+typeOfTraffic+'&ipaddr='+ipAddr);
});
$("#WAFTest").on('load', function () {
alert("Test done");
});
});
Everytime you call sendTraffic()
you bind a new instance of the event handler, which explains the behavior you describe: the first time you call it you attach a load
event handler, then the iframe
loads and the event fires. 每次调用
sendTraffic()
都会绑定一个事件处理程序的新实例,它解释了您描述的行为:第一次调用它时,您会附加一个load
事件处理程序,然后加载iframe
并触发事件。 The second time you call the function, you bind another copy of the event handler to the load
event, so both handlers execute the next time the iframe
loads. 第二次调用该函数时,将事件处理程序的另一个副本绑定到
load
事件,因此两个处理程序都会在下次加载iframe
时执行。
The solution is pretty simple: just take the event binding outside sendTraffic()
: 解决方案非常简单:只需在
sendTraffic()
之外进行事件绑定:
<script>
function sendTraffic(typeOfTraffic) {
var ipAddr = document.getElementById("serverIpaddr").value;
//alert(ipAddr);
$("#WAFTest").attr("src", '/cgi-bin/test.py?wat='+typeOfTraffic+'&ipaddr='+ipAddr);
}
$("#WAFTest").load(function() {
alert("Test done");
});
</script>
Hope this helps! 希望这可以帮助!
With every call of 随着每一次呼唤
$("#WAFTest").load(function () {
alert("Test done");
});
you are binding a new Event-Listener to the load event of the iframe. 您正在将新的Event-Listener绑定到iframe的load事件。
You have to put the call outside of the function, so it is only called and binded once. 你必须把调用放在函数之外,所以它只被调用并绑定一次。
Edit: 编辑:
To prevent a failure of the code, when the DOM is not ready when the code-snippet is called, you have to wrap it in the ready
function of jQuery 为了防止代码失败,当调用代码片段时DOM没有准备好,你必须将它包装在jQuery的
ready
函数中
$(document).ready(function(){
$("#WAFTest").load(function () {
alert("Test done");
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.