Skip to content Skip to sidebar Skip to footer

Jquery Function After Page Redirect?

Check out the fiddle! The Fiddle Im wanting to be able to link to the different sections of the content areas via links similar to the footer links in the example. However I want t

Solution 1:

There is actually no good method to prevent the default hash jump execution especially on page load as each browser has a different behavior on this.

On chrome I tried something like this before and it worked, but not on other browsers:

$(window).load(function() {
    $(window).scrollTop(0);
    //and do your animate scrolling
});

But there is a good trick for this, you can pass a hash which does not have the exact id on your html but should have at least the page-id.

I think on your script you don't need to pass the menu argument base on your markup structure and what your want to achieve, just a page parameter would do fine.

Script:

functionshowPage(page) {
    //show the target page and hide siblings
    $("#page" + page).show().siblings().hide();
    $("#link" + page).children().addClass('active').parent()
    .siblings().children().removeClass('active');
}

functionshowAndScroll(page) {
    showPage(page);
    $('html, body').animate({
        scrollTop: $("#link" + page).closest('.content-slide-menu').offset().top
    }, 1000);
}
$(document).ready(function () {
    $(".menu a").click(function () {
        var $this = $(this);
        showPage($this.data("page"));
    });

    $(".content-slide-menu").each(function () {
        showPage($(this).find("a").first().data('page'));
    });
    //on DOM ready get the hash value and extract the page id number var hash = window.location.hash,
        page = hash.replace(/[^0-9]/g, '');
    //then execute showAndScroll to this pageshowAndScroll(page);

});

On your other pages your link should be like this:

<ahref="page.html#page-2"title="Twit Twoo">Twit Twoo</a>

Jsfiddle demo.

Post a Comment for "Jquery Function After Page Redirect?"