yukatta
yukatta

Reputation: 525

How i can execute a function of Jquery each time page is loaded?

My website is constantly loading content when you go to the bottom of this. And i have the next Jquery (is not important what it does)

$(document).ready(function(){ 
    $(".info").each(function(e) { 
        $(this).html($(this).html().replace('o', 'e')); 
    }); 
});

That script replaces all "o" to "e". The problem is that it only executes once, and i would like that this execute each time that the browser has loaded something, in this case, when you go to the bottom of the website. At the moment, i use this

window.setInterval(function(){
    $(document).ready(function(){ 
        $(".info").each(function(e) { 
        $(this).html($(this).html().replace('ó', 'o')); 
        }); 
    });
}, 6000);

Each 6 seconds, that script is executed but is not an optimal solution. I know that exist a solution based in PHP, but i don't understand PHP yet. Do you know a solution? thanks you in advance

Upvotes: 2

Views: 1267

Answers (1)

Thulasiram
Thulasiram

Reputation: 8552

if you are using jquery ajax and rendered data then,

 $(document).ready(function () {
        $(".info").each(function (e) {
            $(this).html($(this).html().replace('ó', 'o'));
        });

        $('#selector').click(function () {
            $.ajax({
                url: '/your_path', type: 'Get',
                data: {}, //Optional 
                success: function (data) {
                    $('body').append(data);

                    $(".info").each(function (e) {
                        $(this).html($(this).html().replace('ó', 'o'));
                    });
                }
            });
        });
    });

Upvotes: 1

Related Questions