JavaScript Instance Tutorial (5) Setting and checking cookies in a form

Source: Internet
Author: User
Tags requires window zip
The Cookie|cookies|javascript| tutorial cookie is a small piece of information that the browser stores to help identify the user. Setting up and checking cookies in a form requires two files to complete. The first file is cookie1.html, and this file requires some mechanism (a button in the example below) to check if a cookie exists and then redirect it to the form page or the document download page. And the second file, the form page (cookie2.html), is also important because you're going to use a little bit of JavaScript to set up cookies, which is done before submitting the form. Here's a file:

First file (cookie1.html)

<HTML>

<HEAD>

<script language= "JavaScript" >

<!--

function Cookieredirect (Hascookieurl, Nocookieurl)

{

var currentcookie = Document.cookie;


if (Currentcookie.indexof ("Formcomplete=yes")!=-1) {

window.location = Hascookieurl;

} else {

window.location = Nocookieurl;

}

}

-->

</SCRIPT>

</HEAD>

<BODY>

<form name= "Docdownload" >

<input type= "button" value= "Download Document"

>

</FORM>

</BODY>

</HTML>
Second file (cookie2.html) <HTML>

<HEAD>

<script language= "JavaScript" >

<!--

function Sendform (objform)

{

Cookieexpires = "Saturday, 01-jan-03 00:00:00 GMT";

Document.cookie = "Formcomplete=yes; path=/";

Objform.submit ();

}

-->

</SCRIPT>

</HEAD>

<BODY>

<form action= "test.html" name= "info" >

<TABLE>

<TR>

<td>first name</td>

<td><input type= "TEXT" name= "FirstName" ></TD>

</TR>

<td>last name</td>

<td><input type= "TEXT" name= "LastName" ></TD>

</TR>

<TR>

<TD>Address</TD>

<td><input type= "TEXT" name= "Address" ></TD>

</TR>

<TR>

<TD>City</TD>

<td><input type= "TEXT" name= "City" ></TD>

</TR>

<TR>

<TD>State</TD>

<td><input type= "TEXT" name= "state" ></TD>

</TR>

<TR>

<TD>Zip</TD>

<td><input type= "TEXT" name= "Zip" ></TD>

</TR>

</TABLE>

<input type= "button" value= "Download Document"

>

</FORM>

</BODY>

</HTML>


Here are the doc.html files you need to use:

Doc.html

<HTML>

<BODY>


</BODY>

</HTML>

To test this feature, you can open the cookie1.html and click the hit button, and you will be taken to the form page. If you go back to the cookie1.html file and click on the button you will be directly connected to the document.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.