When a user visits below login screen and provides any user name and password, they should be considered a "logged in" user if they go to any subsequent pages without closing their browser.
<html>
<form action="" method="POST">
<span>Login:</span><input type="text" name="puname" ><br>
<span>Password:</span><input type="text" name="ppasswd"><br>
<input type="submit" value="Register"/>
</form>
</body>
</html>
Can anyone suggest how I can implement this with JavaScript (perhaps using cookies)?
asked May 18, 2015 at 21:27
AbuMariam
3,72813 gold badges56 silver badges86 bronze badges
-
You can use cookies or session storage: www.w3schools.com/Html/html5_webstorage.asp. but I think session storage works tab by tabEduardo Pérez– Eduardo Pérez2015年05月18日 21:35:27 +00:00Commented May 18, 2015 at 21:35
-
Are you using a server-side language? Take a look at sessions, or just a simple google searchDowngoat– Downgoat2015年05月18日 22:33:52 +00:00Commented May 18, 2015 at 22:33
1 Answer 1
Cookies are the way to go, you can then make the decision if you want them destroyed at browser close, or destroyed after set time(i.e. 1 day). If your not apposed to jquery, this plugin makes it very simple https://github.com/carhartl/jquery-cookie
answered May 18, 2015 at 21:41
Rob
11.5k10 gold badges41 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Radio
jquery is probably the easiest way to do this. The native JS methods aren't difficult to grasp though. Check out quirksmode.org/js/cookies.html The hardest part is the expiration, and in this case, for a prototype, if you don't set an expiration at all it'll delete when you close the browser.
AbuMariam
How can I get the cookie to get created when they click the 'submit' button on the login form? Do I need to call the JS on the action of the form or the onclick of the submit button? Some examples would be appreciated.
Rob
You need to intercept the submit and run your set cookie. Try this stackoverflow.com/questions/19731265/…
default