There are several mistakes there, I do not know if it solves everything.
Had an assignment operator =
used in place of comparison ==
. No need to ===
because you are sure of the types that are being compared.
was missing if
after the else
to work. It has answers that changes the semantics of the algorithm’s execution by making something run by default, which is different from the original code that you must execute only if you have a specific condition.
Missing a semicolon, it was very disorganized.
Also you can not compare only with the page, have to give the full address, to string that comes from window.location.href
has a full URL, with protocol, domain, port (can be the default), the page file and eventually complements, have to compare with what comes. Could be for example http://www.thi100.com/tela03.html
.
I put a console.log()
there to show what comes, here at Sopt will not work well because of the script running, but running on your page will show which URL is coming. Then you take the information that comes in each scenario and can adapt in condition to be what you want (test on each page you can receive to see each situation, do not try to guess what comes, programming is not guessing game, it is something exact that works with real data collected mechanically and accurately).
var url = window.location.href;
console.log(url);
if (url == 'tela03.html') {
window.location.href = 'tela04.html';
} else if (url == 'tela05.html') {
window.location.href = 'tela06.html';
} else if (url == 'tela07.html') {
window.location.href = 'tela08.html';
} else if (url == 'tela09.html') {
window.location.href = 'tela10.html';
}
I put in the Github for future reference.
I think this is conceptually wrong, but I’m not getting into that merit.
Missed several points. Come on, when there’s a
if
you must buy with 2 or 3 equal==
or===
. The Else command cannot be checked between parentheses.– Leonardo Getulio
It would be ideal for you to debug. You can use the browser pausing row by row to keep track of what is happening. And you can also write some console.log() variables and snippets after the behaviors to see what is being changed. It will help a lot to work this way, mainly using the debugger.
– Leonardo Getulio
Related: Knowing if the number is odd or even
– Icaro Martins
I don’t think your approach is good for doing these redirects, but if you’re going to use the current URL for this, use
Location.pathname
instead ofLocation.href
...– fernandosavio
Do not fix the mistakes you had in the question, this invalidates the answers given, if the error is there people answered based on them, changing the question until it is no longer valid.
– Maniero