简体   繁体   中英

Javascript .onclick function to redirect to other page

Hi i'm trying to validate user typing some data and then redirect the user to other web page, the alerts works nice but the location.href is doing nothing, please help.

window.onload = function(){
    var send = document.getElementById('send');
    var email = document.getElementById('email');       
    var pass = document.getElementById('pass');

    send.onclick = function(){     
        var data1 = email.value;
        var data2 = pass.value;

        if(data1==''){
            alert('Please enter an email address');
        }
        else if(data2==''){
            alert('Please enter your password');
        }
        else{
            window.location.href = 'myotherpage.html';
        }
    }
}    

Thanks.

Solution:

All I needed was to add a return false; after the location to stop the script and continue to the redirection instruction, thanks all for the replies.

window.onload = function(){
var send = document.getElementById('send');
var email = document.getElementById('email');       
var pass = document.getElementById('pass');

send.onclick = function(){     
    var data1 = email.value;
    var data2 = pass.value;

    if(data1==''){
        alert('Please enter an email address');
    }
    else if(data2==''){
        alert('Please enter your password');
    }
    else{
        window.location.href = '/myotherpage.html';
    }
}

}

changed some variables and added a "/" to window.location.href

I changed your code to this and got it working just fine:

window.onload = function(){
    var send = document.getElementById('send');
    var email = document.getElementById('email');       
    var pass = document.getElementById('pass');

    send.onclick = function(){     
        var data1 = email.value;
        var data2 = pass.value;

        if(data1==''){
            alert('Please enter an email address');
        }
        else if(data2==''){
            alert('Please enter your password');
        }
        else{
            window.location = 'myotherpage.html';
        }
    }
}

I changed your valor variables to data , envia to send , and window.location.href to window.location .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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