简体   繁体   English

JavaScript重定向它在服务器上不起作用,但是在本地主机上?

[英]JavaScript redirect it isn't working on server, but on localhost?

So, I've build a little website : http://rolandgroza.com/projects/tcsg/ , but it seems to have a problem that I have not encountered before moving the files to the server. 因此,我建立了一个小网站: http : //rolandgroza.com/projects/tcsg/ ,但是在将文件移动到服务器之前似乎没有遇到过一个问题。 I usually test on localhost before deploying on server, so I only saw the problem after I did. 我通常先在localhost上进行测试,然后再在服务器上进行部署,因此,我这样做后才看到问题。

You can see my JavaScript script in the source code, but I'm placing it here too: 您可以在源代码中看到我的JavaScript脚本,但我也将其放在此处:

$(document).ready(function() {

var header  =   $('body').find('header');
var footer  =   $('body').find('footer');
var badge   =   $('body').find('.badge-wrapper');

var logo_red = $(header).find('.logo a');

var navigation = {
    home:       $('body').find('.home'),
    about:      $('body').find('.about'),
    services:   $('body').find('.services'),
    contact:    $('body').find('.contact')
}

var container = {
    home:       $('body').find('.content-home-wrapper'),
    about:      $('body').find('.content-about-wrapper'),
    services:   $('body').find('.content-services-wrapper'),
    contact:    $('body').find('.content-contact-wrapper')
}

var home = {
    form:       $(container.home).find('.form-wrapper'),
    galleries:  $(container.home).find('.galleries-wrapper')
}

var about = {
    notepad:    $(container.about).find('.notepad-wrapper')
}

var services = {
    minimum:    $(container.services).find('.minimum-package'),
    medium:     $(container.services).find('.medium-package'),
    featured:   $(container.services).find('.featured-package')
}

var contact = {
    notepad:    $(container.contact).find('.notepad-wrapper')
}

var position = function (obj) {
    return {
        'top': (($(window).height() - $(obj).outerHeight()) / 2) + 'px',
        'left': (($(window).width() - $(obj).outerWidth()) / 2) + 'px'
    }
}

var current_page = function() {
    var path = window.location.pathname;
    var current_page = path.substring(path.lastIndexOf('/') + 1);
    return current_page;
}

var curr_pag = current_page();

switch (curr_pag) {
    case 'index.php':
        $(navigation.home).addClass('selected');
        break;
    case 'about.php':
        $(navigation.about).addClass('selected');
        break;
    case 'services.php':
        $(navigation.services).addClass('selected');            
        break;
    case 'contact.php':
        $(navigation.contact).addClass('selected');
        break;
    default:
        break;
}

$(logo_red).on('click', function() {
    var page = current_page();
    switch (page) {
        case 'index.php':
            $(home.form).removeClass('animated flipInX');
            $(home.galleries).removeClass('animated flipInX');
            $(home.form).addClass('animated fadeOutLeftBig');
            $(home.galleries).addClass('animated fadeOutRightBig');
            setTimeout(function(){
                window.location.href = 'index.php';
            }, 2000);
            break;
        case 'about.php':
            $(about.notepad).removeClass('animated flipInX');
            $(about.notepad).addClass('animated flipOutX');
            setTimeout(function(){
                window.location.href = 'index.php';
            }, 2000);
            break;
        case 'services.php':
            $(services.minimum).removeClass('animated flipInY');
            $(services.medium).removeClass('animated flipInY');
            $(services.featured).removeClass('animated flipInY');
            $(services.minimum).addClass('animated fadeOutLeftBig');
            $(services.medium).addClass('animated fadeOutDownBig');
            $(services.featured).addClass('animated fadeOutRightBig');
            setTimeout(function(){
                window.location.href = 'index.php';
            }, 2000);
            break;
        case 'contact.php':
            $(contact.notepad).removeClass('animated flipInX');
            $(contact.notepad).addClass('animated flipOutX');
            setTimeout(function(){
                window.location.href = 'index.php';
            }, 2000);
            break;
        default:
            break;
    }
    e.preventDefault();
})

for(var key in navigation) {
   var object = navigation[key];
   $(object).on('click', function(e){
        var link = $(this).attr('href');
        $(this).addClass('animated hinge');
        var page = current_page();
        switch (page) {
            case 'index.php':
                $(home.form).removeClass('animated flipInX');
                $(home.galleries).removeClass('animated flipInX');
                $(home.form).addClass('animated fadeOutLeftBig');
                $(home.galleries).addClass('animated fadeOutRightBig');
                setTimeout(function(){
                    window.location.href = link;
                }, 2000);
                break;
            case 'about.php':
                $(about.notepad).removeClass('animated flipInX');
                $(about.notepad).addClass('animated flipOutX');
                setTimeout(function(){
                    window.location.href = link;
                }, 2000);
                break;
            case 'services.php':
                $(services.minimum).removeClass('animated flipInY');
                $(services.medium).removeClass('animated flipInY');
                $(services.featured).removeClass('animated flipInY');
                $(services.minimum).addClass('animated fadeOutLeftBig');
                $(services.medium).addClass('animated fadeOutDownBig');
                $(services.featured).addClass('animated fadeOutRightBig');
                setTimeout(function(){
                    window.location.href = link;
                }, 2000);
                break;
            case 'contact.php':
                $(contact.notepad).removeClass('animated flipInX');
                $(contact.notepad).addClass('animated flipOutX');
                setTimeout(function(){
                    window.location.href = link;
                }, 2000);
                break;
            default:
                break;
        }
        e.preventDefault();
    })
}

$(badge).hover(
    function(){
        $(this).addClass('animated swing');
    },
    function(){
        $(this).removeClass('animated swing');
    }
);

setTimeout(function(){
    $(header).addClass('animated fadeInDown');
    $(footer).addClass('animated fadeInUp');
    $(badge).addClass('animated swing');
}, 1000);

setTimeout(function(){
    $(home.form).addClass('animated flipInX');
    setTimeout(function(){
        $(home.galleries).addClass('animated flipInX');
    }, 500);
}, 2000);

setTimeout(function(){
    $(about.notepad).addClass('animated flipInX');
}, 2000);

setTimeout(function(){
    $(services.minimum).addClass('animated flipInY');
    setTimeout(function(){
        $(services.medium).addClass('animated flipInY');
        setTimeout(function(){
            $(services.featured).addClass('animated flipInY');
        }, 500);
    }, 500);
}, 2000);

setTimeout(function(){
    $(contact.notepad).addClass('animated flipInX');
}, 2000);

for(var key in container) {
   var object = container[key];
   var css = position(object);
   $(object).css(css);
}

$('select').dropkick();

});

The problem is that when clicking one of the menu links there should be some animations happening and then redirected to a the link you clicked on, with window.location.href , but it doesn't work, almost anything. 问题是,单击菜单链接之一时,应该发生一些动画,然后使用window.location.href将其重定向到您单击的链接,但几乎没有任何效果。

I cannot tell what is the problem so I'm hoping someone can, because on XAMPP localhost works fine. 我不能说出问题是什么,所以我希望有人可以,因为在XAMPP本地主机上可以正常工作。

The problem is in that your asking the current page, and on your home page when the index.php isn't in the url your missing something and your switch cases arn't working. 问题在于您询问当前页面,以及当index.php不在url中时您的主页上,您丢失了某些内容,并且切换用例无法正常工作。 Add to your switch function in the navigation a case for having no page or page == '' that way it will know what to do. 在导航中添加没有页面或页面==''的情况,这样它将知道该怎么做。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM