Changing window.location.href does not redirect to the new page

Asked

Viewed 32,392 times

0

if it is working but the window.location 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";
    } 
}
  • You want it to go to an internal or external page?

  • You want me to go to an external or internal page?

  • 1

    Just try using window.location, with the http (if it is external page).

  • This type of behavior is peculiar when, for example, the button that triggers the redirect routine is inside a <form> with Submit, or even in the lines below the </form>. One solution is to put the button (or link) above the <form tag>

3 answers

4

When it comes to external links you have to put 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>

  • Your code does not work at least on Chrome and mozila does not google.

1

Depending on what you would like, just redirect without the person even seeing the content of the page, it is better to use window.location.replace('//sitegenerico.com') because in such cases the browser does not even save the site that requested the redirect in the person’s history. If you want to switch pages as you click on a link, you should use window.location.href = '//sitegenerico.com'.

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

0

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

Script:

console.log(window.location.href);

Return:
/questions/129417/windows-location-n%c3%a3o-funciona

Notice that the return had with http:// thus to assign a new value to the window.location.href, we should also put the protocol http, as this references a new URL, if it does not place it will understand that it is an internal page of your site.

According to your code, without the http in the 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.

  • Your code does not work on Chrome or mozila does not work on both

  • You can [Edit] your question and ask how you are calling this function, because I did some tests here and it worked, the example of the @Pedro answer, works here too, better contextualize your problem, so we can solve it in the best way possible :)

  • I used Pedro’s code through "Execute code snippet" and nothing happened but that I tested in my own code and nothing happened again. Could this be a problem for the browsers I’m using? Mozilla and Chrome

  • tests the command via browser console, in case without the function only the main, window.location.href="http://www.google.pt"; and see if any errors return

  • Comment on console works

  • presents some error ?

  • There is no mistake

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.