Alvaro Bascunan
Alvaro Bascunan

Reputation: 81

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.

Upvotes: 1

Views: 279

Answers (2)

PedroPombo
PedroPombo

Reputation: 23

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

Upvotes: 1

Kodie Grantham
Kodie Grantham

Reputation: 2031

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.

Upvotes: 0

Related Questions