Trovin
2/13/2019 - 4:31 PM

Redirect & anchor scroll

JS = Redirect & anchor scroll

    //redirect & anchor scroll
    var host = window.location.hostname;

    function anchorRedirect() {
        var parhToBlogPage = '/blog/#to-blog';
        var parhToNewsPage = '/blog/#to-news';

        var anchorItemBlog = $('.js-link_blog');
        var anchorLinkBlog = $('.js-link_blog').find('a');

        var anchorItemNews = $('.js-link_news');
        var anchorLinkNews = $('.js-link_news').find('a');


        anchorItemBlog.on('click', function (e) {
            e.preventDefault();
            var host = window.location.hostname;
            var pathToRedirect = 'http://' + host + parhToBlogPage;
            window.location.replace(pathToRedirect);
        });

        anchorItemNews.on('click', function (e) {
            e.preventDefault();
            var pathToRedirect = 'http://' + host + parhToNewsPage;
            window.location.replace(pathToRedirect);
        });

        //check redirect
        function checkRedirect() {
            var currentLocation = location.href;
            var pathToBlogPage = 'http://' + host + parhToBlogPage;
            var pathToNewsPage = 'http://' + host + parhToNewsPage;

            if(currentLocation == pathToBlogPage) {
                var destination = $('#blog').offset().top;
                $('html, body').animate({
                    scrollTop: destination
                }, 400);
            }
            else if(currentLocation == pathToNewsPage) {
                var destination = $('#news').offset().top;
                $('html, body').animate({
                    scrollTop: destination
                }, 800);
            }
        }
        checkRedirect();
    }