1

I have read through a number of posts on this and tried different approaches but am still having trouble. I have an html form in a php page which has an onClick command to run a javascript validation procedure when the user clicks a button on the screen. This all works fine, but at the end of the validation I want to open a new php page in the same tab based on the value of a customURL variable (which does contain the correct URL). But the page will only reload itself,not the new page. I have tried window.location.href, location.href and document.location. Does anyone have any ideas why none of these work, or can suggest an alternative approach? Thank you.

if(optionChosen == "overview") { customURL ='overview.php'; }
if(optionChosen == "tasks")    { customURL ='tasks.php';}
if(optionChosen == "guidance") { customURL ='guidance.php';}
if(optionChosen == "feedback") { customURL ='feedback.php';}
if(optionChosen == "progress") { customURL ='progress.php';}

customURL = customURL + "?uid=" + unitChosen;
document.location = customURL;
Mark Leighton
  • 101
  • 1
  • 2
  • 10
  • 1
    Try this http://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript – Sivakumar May 27 '14 at 11:34
  • You have to use `window.open(customURL,'_blank');` instead of `document.location = customURL;` – Think Different May 27 '14 at 11:35
  • 1
    `window.location.href = customURL` should work. If it isn't, open up the browser console and see if anything is being printed out – asprin May 27 '14 at 11:35
  • possible duplicate of [Java Script is not redirecting using window.location](http://stackoverflow.com/questions/7887196/java-script-is-not-redirecting-using-window-location) – Rakesh Shetty May 27 '14 at 11:43
  • Thanks guys - but no luck. window.open at least opens the new page, but in a new tab which I don't want - it has to be in the existing tab. window.location and window.location.href just seems to reloas the existing page in the same tab. – Mark Leighton May 27 '14 at 11:45

4 Answers4

3

First make sure that anyone of the options is selected or write the code like this

if(optionChosen == "overview") { customURL ='overview.php'; }
else if(optionChosen == "tasks")    { customURL ='tasks.php';}
else if(optionChosen == "guidance") { customURL ='guidance.php';}
else if(optionChosen == "feedback") { customURL ='feedback.php';}
else { customURL ='progress.php';}

Use this to avoid redirects when a user clicks "back" in their browser

window.location.replace('http://somewhereelse.com');

Use this to redirect, a back button call will trigger the redirection again

window.location.href = "http://somewhereelse.com";

Then use any of this following method as per your requirements.


Given for completeness, essentially an alias to window.location.href

window.location = "http://somewhereelse.com";
AeJey
  • 1,447
  • 20
  • 40
  • thanks AaJey I will amend this, although it is not what is at fault here as I have checked that the correct URL is being placed in the customURL variable. Just need to find a way of displaying the new page in the same tab - no luck yet depsite the suggestions made, – Mark Leighton May 27 '14 at 12:28
2

do window.location.href = customURL;

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
Mir Adnan
  • 844
  • 11
  • 24
  • @nl-x A comment alone would have sufficed – Rakesh Shetty May 27 '14 at 11:40
  • @nl-x yes it is and it also proves how poor the question is. A simple Google search would have given him the correct syntax. – asprin May 27 '14 at 11:41
  • Thanks I am aware of the syntax.. window.location /window.location.href / window.location.replace / document.location / all reload the same page in the tab. window.open opens the new page but in a new tab. I can't work out why this is the case. – Mark Leighton May 27 '14 at 12:00
1

it is window.location , not document.location , and to be more precise it's actually window.location.href

nl-x
  • 11,762
  • 7
  • 33
  • 61
0

Thanks all - found a new approach in the end which is working for me - using document.input.action = customURL; and returning to the input form. So will stick with this. Appreciate your suggestions and help.

Mark Leighton
  • 101
  • 1
  • 2
  • 10