Perform a redirect based on the "confirm"

2

What I want to do is redirect to another page once the user confirms that action.

The code I have so far is the following:

window.onload = function(){
        var pagar = document.getElementById(claspagar); 
        claspagar.onclick = function(){(if confirm (location.href = ("checkout.html"))};
             else("Continua comprando")
             }
        }

I am a newbie and I am learning on my own, I would like to know if it is implemented in the correct way, in the Dreamweaver it marks me a syntax error.

    
asked by Fabricio 13.09.2016 в 00:21
source

2 answers

0

We will look at the code line by line to see what is right and what is (or may be) wrong:

window.onload = function(){

This line has no fault with the naked eye.

    var pagar = document.getElementById(claspagar); 

This line may contain a failure. claspagar is a variable, if it is not defined, you will receive an error here. If it is defined, then pay will have the element with ID defined in variable claspagar .

The following parts I will separate into different lines to analyze them little by little:

    claspagar.onclick = function(){

This is an error, or it was the previous step. So claspagar is a string with the ID of an element, then it can not be assigned an event onclick , if it is an element it can be assigned a click handler, but then the previous step would fail. What I imagine is that instead of claspage.onclick what you want to do is pagar.onclick .

      (if confirm (location.href = ("checkout.html"))

The syntax of this is incorrect. Checking the if should go in parentheses, in addition, there are three opening parentheses and only two closing parentheses.

    };

This closing would correspond to the claspagar.onclick function, which will make the next part incorrect:

         else("Continua comprando")
         }

This code is orphaned. I should go with if , but since it was closed incorrectly, it does not make sense. Also, ("Continue buying") is just a string and does not make sense on its own, I guess you want it to go with a console.log or a alert . The closing key does not correspond to any opening key (one is missing in else ).

    }

This would be the closing of the window.onload function and it's fine.

Solving the errors above, the code would look like this (assuming that claspagar is a variable with the name of the ID, if it is the name itself, you should quote it):

window.onload = function(){
    var pagar = document.getElementById(claspagar); 
    pagar.onclick = function(){
        if (confirm("Estás seguro?")) {
            location.href = "checkout.html";
        } else {
            alert("Continua comprando");
        }
    }
}
    
answered by 13.09.2016 / 01:06
source
0

Since you want to get an address to another web page the confirm is poorly implemented, what you want to do could be done like this:

window.onload = function(){
  var r = confirm("Seguir comprando?");
  if (r == true) {
    alert("Confirm aceptado"); //alert para comprobar la opción elegida
    //aqui haces la redirección, podrias usa location.href

    /************REDIRECCION**********/
    //window.location.href = "http://ejemplo.com";
    
  } else {
    alert("Confirm cancelado");
  }
}
    
answered by 13.09.2016 в 00:48