Search code examples
javascriptjqueryjquery-eventsonload-event

How to run a Javascript function everytime f5 is used to refresh the page


The following code works for the first time that the page is loaded but when the page is refreshed using F5, it does not work on Firefox. In Chrome it works fine.

$(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...");
        });
    });
});

Solution

  • This code works fine for me

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