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';
}
asked Aug 23, 2015 at 1:53
2
  • Using Javascript, your only need to use window.location.href='http://yoururl/' for the redirect. See stackoverflow.com/questions/7077770/… Commented Aug 23, 2015 at 1: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] (python.org/downloads) [here] So you would be able to run your application just by visiting localhost:8080 which make things easier Commented Aug 23, 2015 at 18:41

1 Answer 1

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";
answered Aug 23, 2015 at 1:54
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.