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");
}
}
}