-2

I'm trying to do what I think is a really simple thing, and I can't find any working solutions online. Basically, I want links in if statements. The pages I want to navigate to are in the same folder as the index-page.

Here are the if-statements:

if(cartman > kyle && cartman > stan && cartman > kenny && cartman > butters){
    window.location = 'cartman.html';
}
else if(kyle > cartman && kyle > stan && kyle > kenny && kyle > butters){
    window.location = 'kyle.html';
}
else if(stan > cartman && stan > kyle && stan > kenny && stan > butters){
    window.location = 'stan.html';
}
else if(kenny > cartman && kenny > stan && kenny > kyle && kenny > butters){
    window.location = 'kenny.html';
}

I have found many different codes online that are supposed to do this, but none of them have worked. What is the correct way to use javascript to navigate to another page?

Magsen85
  • 45
  • 7

2 Answers2

0

Try this:

if(x==1){
    window.location = 'newurl1.html';
}
else if(x==2){
    window.location = 'newurl2.html';
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Spock
  • 4,700
  • 2
  • 16
  • 21
  • I'm afraid that one doesn't work either. The this is, if I replace the code where with the link with a simple alert() code, the program works. The alert() window will pop up, so I know that the rest of the code works. – Magsen85 Dec 13 '14 at 03:05
  • must be a cross browser thing... As a matter of interest, what browser are you on? – Spock Dec 13 '14 at 06:12
0

Try using window.location.replace:

var x = parseInt(prompt();)
if(x === 0) {
    window.location.replace("http://stackoverflow.com");
} else if (x == 1) {
    window.location.replace("http://google.ca");
}

Rather than the methods you have tried, window.location.replace stimulates a redirect, and pretends you have never left the original page. If you want to stimulate clicking a link, use window.location.href.

For more information, see here: How to redirect to another webpage in JavaScript/jQuery?

Community
  • 1
  • 1
unbindall
  • 514
  • 1
  • 13
  • 29