Change window.location.href does not redirect to new page

0

The if is working but window.location is not going to google is just an example but I think it explains the error.

function eliminaParagem (){
    var confirma =confirm("Tem a certeza que quer eliminar a paragem");
    if (confirma==true){
        window.location.href="wwww.google.pt";
    } 
}
    
asked by anonymous 19.05.2016 / 15:55

3 answers

3

When it comes to external links, you have to http:// before the address.

function eliminaParagem (){
    var confirma =confirm("Tem a certeza que quer eliminar a paragem");
    if (confirma==true){
        window.location.href="http://www.google.pt";
    } 
}
<button onclick="eliminaParagem();">Google</button>
    
19.05.2016 / 16:16
1

Depending on what you like, just redirect without the person or see the content of the page, it is better to use window.location.replace('//sitegenerico.com') because in these cases the browser does not save the site that requested the redirection in the person's history. If you want to change pages as you click on a link it is best to use window.location.href = '//sitegenerico.com' .

When changing the page to an external site it is advisable to use a protocol-less link (without http: or https: ), only the two // . Google example: //google.com

    
19.05.2016 / 18:51
0

The window.location.href property returns the URL of the current page, for example, if I open to put the command below in the page of that question, we will have the return of the URL of that page:

Script:

console.log(window.location.href);

Return:
http://pt.stackoverflow.com/questions/129417/windows-location-n%c3%a3o-funciona

Notice that the return has http:// to assign a new value to window.location.href , we should also put the http protocol, therefore it references a new URL, if it does not put it, it will understand that it is a page inside your site.

As your code, without http up front would return something like this:

https://www.seusite.com/www.google.pt

Making the change your script looks like this:

function eliminaParagem (){
    var confirma =confirm("Tem a certeza que quer eliminar a paragem");
    if (confirma==true){
        window.location.href="http://www.google.pt";
    } 
}

This way redirecting to another site.

    
19.05.2016 / 16:25