【问题标题】:How to run a Javascript function everytime f5 is used to refresh the page每次使用 f5 刷新页面时如何运行 Javascript 函数
【发布时间】:2015-07-09 15:29:32
【问题描述】:

以下代码在第一次加载页面时有效,但是当使用 F5 刷新页面时,它在 Firefox 上不起作用。在 Chrome 中它工作正常。

$(document).ready(function() {
    $("iframe").each(function() {
        // Using closures to capture each one
        var iframe = $(this);
        iframe.on("load", function() { // Make sure it is fully loaded
            iframe.contents().click(function(event) {
                iframe.trigger("click");
            });
        });

        iframe.click(function() {
            alert("Clicked...");
        });
    });
});

【问题讨论】:

标签: javascript jquery jquery-events onload-event


【解决方案1】:

这段代码对我来说很好用

$(document).ready(function () {
    document.getElementById('main_iframe').contentWindow.location.reload(true);
    $('iframe').load(function () {
        $(this).contents().find("body").on('click', function (event) {
            alert("clicked....")
        });
    });
});

【讨论】:

    【解决方案2】:

    将所有代码放入jQuery(function(){ //here }),一切都会好起来的。当页面加载时运行,每个 f5 操作,删除其他代码以测试重新加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2019-08-06
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多