LoginSignup
4
4

More than 5 years have passed since last update.

ifameのload eventが複数回発生する

Posted at

説明がむずかしいのですが、以下のようなコードがあったとします。

$(document).ready(function () {
    $("#MyIframe").load(function () {
        alert("ifram loaded!!");
    });

    $("foo").bind("click", function () {
        $("#MyIframe").attr("src", "http://example.com");
    });
});

1回目のクリックはアラート表示は1回だけですが、2回目のクリックは2回、n回目のクリックはn回アラート表示が表示されます。

とりあえず以下のように変更しましたが、これが原因だよ、とか、こうしたらいいよ、というのがあれば教えてくださいませ。

$(document).ready(function () {
    var isLoaded = false;
    $("#MyIframe").load(function () {
        if (isLoaded) {return false}
        alert("ifram loaded!!");
        isLoaded = true;
    });

    $("foo").bind("click", function () {
        $("#MyIframe").attr("src", "http://example.com");
    });
});
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4