Search code examples
javascriptjqueryajaxfirefoxgoogle-ad-manager

Firefox back button not working properly with google ads


I have created a website in which I load the page using ajax. I have used pushstate method to change the url. When I traverse through website and then click on back button nothing happens. Now when I install AdBlock everything works fine. Site is working fine on chrome.

$(document).on('ready readyAgain', function () {
function ChangeUrl(title, url) {
    if (typeof (history.pushState) != "undefined") {
         var obj = { Title: title, Url: url };
         history.pushState(obj, obj.Title, obj.Url);
    } else {
        alert("Browser does not support HTML5.");
    }
}

function loadPrevPage(ajax_url) {

    $.ajax({
        url: ajax_url,
        type: "GET",
        dataType: "html"
    }).done(function (data, textStatus, jqXHR) {
       var $divToFind = $($.parseHTML(jqXHR.responseText)).filter("#radio-main-wrapper");
        $('#radio-main-wrapper').html($($divToFind).html());
        var reponse = jQuery(jqXHR.responseText);
        var reponseScript = reponse.find(".all_wrapper script");
        jQuery.each(reponseScript, function (idx, val) {
            var parent_div = $(this).parent();
            window.ad_div = parent_div[0];
            if (val.text === "") {
                var src = val.src;
                $.getScript(src);
            } else {
                $.globalEval(val.text);
            }

        });
       $(document).trigger('readyAgain');
    }).fail(function (jqXHR, textStatus, errorThrown) {

    });
}

window.onpopstate = function (e) {
    loadPrevPage(location.pathname);
}

$("body a").click(function (e) {
    e.preventDefault();
       ChangeUrl('', ajax_url);
         $.ajax({
                    url: ajax_url,
                    type: "GET",
                    dataType: "html"
                }).done(function (data, textStatus, jqXHR) {
                  var $divToFind = $($.parseHTML(jqXHR.responseText)).filter("#radio-main-wrapper");
                    $('#radio-main-wrapper').html($($divToFind).html());
                    var reponse = jQuery(jqXHR.responseText);
                    var reponseScript = reponse.find(".all_wrapper script");
                    jQuery.each(reponseScript, function (idx, val) {
                        var parent_div = $(this).parent();
                        window.ad_div = parent_div[0];
                        if (val.text === "") {
                            var src = val.src;
                            $.getScript(src);
                        } else {
                            $.globalEval(val.text);
                        }
                    });
                    $(document).trigger('readyAgain');
                }).fail(function (jqXHR, textStatus, errorThrown) {

                });
    });
});

You can check the code over here -- http://www.planetradiocity.com/popstate/

When I go to test1 or test3, I will have to click on the back button twice.


Solution

  • Every time browser loads the google ad it will push page into the history object. You can try loading google ads in iframe.