Not able to redirect to "index.html" after doing something with data. Find HERE in my code. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Not able to redirect to "index.html" after doing something with data. Find HERE in my code.

form syntax: ___ <body> <form id="formId" onsubmit="addStudentData()"></form> </body> __ Scripts: __ function addStudentData() { let name = document.getElementById('sname').value; let id = document.getElementById('sid').value; let year = document.getElementById('syear').value; let stream = document.getElementById('sstream').value; /* Form validation -- the fields should not be empty. */ if (name == null || name == "", id == null || id == "", year == null || year == "", stream == null || stream == "") { alert("Please fill all required fields."); return false; } /* Making a JSON object using the form values */ let studentObj = { 'name': name, 'year': year, 'stream': stream }; /* Pushing record to sessionStorage after stringify JSON Object*/ sessionStorage.setItem(id, JSON.stringify(studentObj)); /* Redirecting back to home page*/ alert("Done. Press OK, redirect to home page."); //HERE HERE HERE window.location = "index.html"; }

19th Mar 2018, 2:17 PM
Ashutosh
Ashutosh - avatar
5 Respostas
+ 4
Try to specify an absolute url, rather than a relative one (using "http://domain-name.ext/file" instead just "file")... Especially if you're are testing the code in sololearn playground ^^ Else try other ways to redirect, but maybe you must specify what's happened and/or what error you get instead of redirection... https://developer.mozilla.org/en-US/docs/Web/API/Window/location https://www.w3schools.com/jsref/prop_loc_href.asp
20th Mar 2018, 3:02 AM
visph
visph - avatar
+ 1
Tried, but not working.
19th Mar 2018, 2:54 PM
Ashutosh
Ashutosh - avatar
+ 1
the return keyword was missing in the onsubmit="return addStudent()";..it's now working thanks for the help.
21st Mar 2018, 6:16 PM
Ashutosh
Ashutosh - avatar
0
try adding a href to the window.location, like the below. window.location.href = "index.html"; or window.location.href = "http://your-domain.com/index.html";
19th Mar 2018, 2:26 PM
ihateonions
0
window.location.replace('http://example.com'); Itā€™s better than using window.location.href = ā€˜http://example.comā€™; Using replace() is better because it does not keep the originating page in the session history, meaning the user wonā€™t get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use window.location.href If you want to simulate an HTTP redirect, use window.location.replace You can use assign() and replace methods also to javascript redirect to other pages like the following: location.assign("http://example.com"); The difference between replace() method and assign() method(), is that replace() removes the URL of the current document from the document history, means it is not possible to use the ā€œbackā€ button to navigate back to the original document. So Use the assign() method if you want to load a new document, andwant to give the option to navigate back to the original document. http://net-informations.com/js/iq/load.htm
7th Nov 2022, 6:30 AM
fostercarly