Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting redirected to another webpage on page load with the contents of my JavaScript array as the URL

I am doing a small exercise using HTML and Javascript. Basically, I have 2 input fields that I need to validate using 1 function in Javascript. The first input asks for the name and if the name is less than 3 characters, there would be an error printed, else, the error is removed. The second one asks for the locality. Now, to validate this, I must first create an array which contains about 3 locations and then compare the location the user entered to see if it is in the array. If it is not, again, an error is printed. The thing is this; I am getting an error on live preview. The error prints "Cannot GET /Msida,Pieta,Marsa" on screen and "Failed to load resource: the server responded with a status of 404 (Not Found)" on the console. What could the problem be?

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Revision</title>
</head>

    <body>
        <nav id="menu"></nav>
        <a>Full Name: </a><input type="text" name="fName" id="fName" ><br/>
        <div id="divName"></div><br/>
        <a>Locality: </a><input type="text" name="locality" id="locality" ><br/>
        <div id="divLocality"></div><br/>
        <script src="js/mainScript.js"></script> 
        <script src="js/detailsScript.js"></script> 
    </body>
</html>

JavaScript:

var fName = document.getElementById("fName");
var locality = document.getElementById("locality");
var divName = document.getElementById("divName");
var divLocality = document.getElementById("divLocality");
var location = ["Msida", "Pieta", "Marsa"];


function validation()
{
if(this.name == "fName")
    {
        if(this.value.length < 3)
            {
                divName.innerHTML = "Name should have atleast 3 characters";
                divName.style.color = "red";
            }
        else if(this.value.length >= 3)
            {
                divName.innerHTML = "";
            }
    }
else if(this.name == "locality")
    {
        if(location.indexOf(this.value) > -1)
        {
            divLocality.innerHTML = "Location Exists";
            divLocality.style.color = "red";
        }
        else
        {
            divLocality.innerHTML = "Location doesn't Exist";
            divLocality.style.color = "red";
        }
    }

}

fName.addEventListener('focusout', validation);
locality.addEventListener('focusout', validation);

Thanks!

like image 252
johnny Avatar asked May 20 '18 20:05

johnny


People also ask

How Google treat JavaScript redirects?

JavaScript location redirectsOnly use JavaScript redirects if you can't do server side or meta refresh redirects. While Google attempts to render every URL Googlebot crawled, rendering may fail for various reasons.

How do you redirect to another page in JavaScript with get data?

To redirect to another page in JavaScript, you can use window. location. href property, window. location will also work and if you want you can use window.


1 Answers

Just rename your location variable. Your current code is not working because you are basically re-assigning the URL. See window.location.

JavaScript will actually execute var location = ['a', 'b', 'c']; in 2 parts, declaration and assignment. The declaration part (var location) will be simply ignored because location is always declared initially (at least in the browser). And the re-assignment part (location = ....) will redirect you to location.toString.

like image 109
doubleOrt Avatar answered Sep 30 '22 05:09

doubleOrt