Go to certain link depending on the page

Asked

Viewed 431 times

1

I’m developing a quiz with several themes, but it’s time to put it to a certain page depending on which page the person is on. I thought I’d use the if but he always goes for the last if, I’ll put the code here. Does anyone know where I went wrong?

var url = window.location.href;

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 (url = 'tela09.html'){
  window.location.href = 'tela10.html'
}
  • 2

    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.

  • 1

    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.

  • 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 of Location.href...

  • 2

    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.

5 answers

1


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.

1

The correction of your code should look +/- like this:

var url = window.location.href;

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 {
  window.location.href = 'tela10.html'
}

or better could use switch:

var url = window.location.href;
switch (url) {
    case 'tela03.html':
        window.location.href = 'tela04.html';
        break;
    case 'tela05.html':
        window.location.href = 'tela06.html';
        break;
    case 'tela07.html':
        window.location.href = 'tela08.html';
        break;
    defult:
        window.location.href = 'tela10.html';
}

0

its conditions of if are incorrect... You just changed the value of the variable url under the conditions if, and the variable did not return any value booleano, therefore all conditions if were ignored...

And another: the code window.location.href returns the url you are in... and not the current directory file...

If your url is http://localhost/project/tela03.html, then var url = window.location.href; will return the value http://localhost/project/tela03.html and not tela03.html.

Then your code if should be more or less like this:

if(url == 'url que você está')

0

The location.href returns the whole URL and not just the current page. Instead of redirecting to a page based on the URL or page name, in my view it would be more practical to insert a variable on each page that identifies it.

For example, on the page tela03.html put a variable in the script:

<script>var tela = 3;</script>

On the page tela04.html place:

<script>var tela = 4;</script>

And so on every page.

And create a .js external and call it at the end of the body of each page:

<script src="redirecionamento.js"></script>

In the archive redirecionamento.js you make the if's, by checking the variable tela to know which page you are on and where you are going:

if(tela == 3){
   location = "tela04.html";
}else if(tela == 4){
   location = "tela05.html";
}
etc...

You can also do something more dynamic, without using if's, just incrementing the variable tela with +1 and redirect to the next page:

location = "tela"+ ("0" + (tela + 1)).slice(-2) +".html";

If you have it on the page tela03.html, will go to the page tela04.html and so on.

-1

In the condition of your if/Else you are always assigning a value instead of making a comparison.

For example, where is:

if ( url = 'pagina.html' )

should be:

if ( url == 'página.html' )

Moreover, window.location.href return the current url this way:

https://site.com.br/pagina.html

To do what you want, you could use window.location.pathname, that would return this way:

/pagina.html

Applying these changes:

var url = window.location.pathname;

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( url == '/tela09.html '){
  window.location.href = 'tela10.html'
}

Browser other questions tagged

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