0

I'm trying to do an exercise that will allow users to be redirected to a specific local web page based on their search term. For some reason, window.location.replace will not redirect. I tried a variant of window.location that opened the landing page in a new tab, which worked. Any help is appreciated!

html

<form name="form1" onsubmit="myFunction()">
Street Address:
<input type="text" id="streetaddress" required="true"><br>
Township:
<input type="text" id="township" required="true">
<br>
<input type="submit" onclick="return myFunction()" name="Search" value="Search">
</form>
<p id="demo"></p>

js

<script>
function myFunction() {
document.getElementById("demo").innerHTML = "";
var nameValue1 = document.getElementById("streetaddress").value;
var nameValue2 = document.getElementById("township").value;

if (nameValue1.toUpperCase() === "1 MARJORAM DRIVE" && nameValue2.toUpperCase() === "LUMBERTON") {
window.location.replace("landingpage.html");
}
else {
document.getElementById("demo").innerHTML = "<font color='red'><p>0 results</p></font>";
return false;
}
}
</script>

Update

I have modified my code implementing suggested solutions but am still encountering the same issue where the page won't redirect. Lines I've changed from original code are below...

html

<form name="form1" onSubmit="return myFunction()">

<input type="submit" name="submit" class="submit" value="Search">

js

if (nameValue1.toUpperCase() === "1 MARJORAM DRIVE" && nameValue2.toUpperCase() === "LUMBERTON") {
window.location.href = 'landingpage.html';
}
Don
  • 1
  • 1
  • 1
  • 2
  • Using Javascript, your only need to use `window.location.href='http://yoururl/'` for the redirect. See http://stackoverflow.com/questions/7077770/window-location-href-and-window-open-methods-in-javascript – Sablefoste Aug 23 '15 at 01:59
  • Try 1. Check that your condition `if` is being `true` you can check by adding a `console.log('yes')` to your if statement block, Try 2. add a `return` to `window.location.(any format you use)`. Try 3. Download a python package [here] (https://www.python.org/downloads/) [here] So you would be able to run your application just by visiting `localhost:8080` which make things easier – Babajide Fowotade Aug 23 '15 at 18:41

1 Answers1

1

Just change the window.location variable

window.location = "http://www.newsite.com/path/to/page";

For a page that is locally hosted use the following:

window.location.href = "newlocation.html";
asdf
  • 2,927
  • 2
  • 21
  • 42
  • It's not hosted, just html/js/css project so I'm not running apache on localhost either. I just need it to change to a local html page in the same folder if it matches a search term defined. – Don Aug 23 '15 at 01:58
  • window.location.href is not working. When I submit the form with the information it's looking for, it stays on the same page. The url looks like file:///E:/wamp/www/TJ/html/index.html?Search=Search – Don Aug 23 '15 at 05:41