Home > Back-end >  The window.location tag is not redirecting to next page
The window.location tag is not redirecting to next page

Time:12-13

I am making a page in which a user inputs details and when he submits, the page should be redirected to another page showing those details. But the problem is that the window.location is not redirecting it to next page.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="Contact.css"/>
    <title>Document</title>
</head>
<body>
    <main id="main">
        <section>
            <h1 id="heading">
                CONTACT US
            </h1>
            <form id="contact" onsubmit="NextPage();return false">
                <input type="text" name="name" placeholder="Name" id="name" required/>
                <input type="email" name="email" placeholder="E-mail" id="email" required/>
                <input type="text" name="sub" placeholder="Subject" id="subject" required/>
                <input type="text" name="mess" placeholder="Your Message" id="message" required/>
                <textarea rows="5" maxlength="300" placeholder="Type your queries here" name="query" id="query"></textarea>
                <button type="submit" name="submit" onclick="NextPage()" id="submit">Submit</button>
            </form>
        </section>
    </main>
</body>
</html>

JS

function NextPage(){
    var contactName = document.getElementById("name");
    var contactEmail = document.getElementById("email");
    var contactSubject = document.getElementById("subject");
    var contactMessage = document.getElementById("message");
    var contactQuery = document.getElementById("query");
    var Name = contactName.value;
    var Email = contactEmail.value;
    var Subject = contactSubject.value;
    var Message = contactMessage.value;
    var Query = contactQuery.value;
    sessionStorage.setItem("Name1" , "Name");
    sessionStorage.setItem("Email1" , "Email");
    sessionStorage.setItem("Subject1" , "Subject");
    sessionStorage.setItem("Message1" , "Message");
    sessionStorage.setItem("Query1" , "Query");
    // return=false;
    window.location.replace=("Welcome.html");
}

I also looked for similar questions on stackowerflow but none of the worked I don't know why. Please look into this as I am new in web designing.

Edit 1:

I tried alert before window.location, even that is not working

    alert("Your query have been submitted.");
    window.location.replace=("Welcome.html");
//at the end in JS

Edit3: I am sorry, I am very sorry. I am so stupid I didn't link the script sheet to main html. I am just blind af.

CodePudding user response:

window.location.replace is a function: you cannot set data to it - you can only call it to make it do stuff. When calling it, you don't need the equals sign in there. If you put the equals sign in there, you're changing the function do something else when it's called... not calling it to do what you want.

You need to remove the equal sign:

window.location.replace("/Welcome.html");

CodePudding user response:

I don't think window.location.replace=("location") is a good idea, I personally don't use it. I use window.location="location".It's easier.

But if you want to use exactly this method, you might want to google it.

Hope that helps, OpiliteX

CodePudding user response:

*You can use one of the following codes*



window.location.href = "Welcome.html";
// or
window.location.pathname = "/Welcome.html";
  • Related