javascript实例教程(5) 在一个表单中设置和检查Cookies
javascript实例教程(5) 在一个表单中设置和检查Cookies,javascript实例教程(5) 在一个表单中设置和检查Cookies
第一个文件(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"
onClick="cookieRedirect('doc.html', 'cookie2.html')"
</FORM
</BODY
</HTML
第二个文件(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
<TDFirst name</TD
<TD<INPUT TYPE="TEXT" NAME="firstname"</TD
</TR
<TDLast name</TD
<TD<INPUT TYPE="TEXT" NAME="lastname"</TD
</TR
<TR
<TDAddress</TD
<TD<INPUT TYPE="TEXT" NAME="address"</TD
</TR
<TR
<TDCity</TD
<TD<INPUT TYPE="TEXT" NAME="city"</TD
</TR
<TR
<TDState</TD
<TD<INPUT TYPE="TEXT" NAME="state"</TD
</TR
<TR
<TDZip</TD
<TD<INPUT TYPE="TEXT" NAME="zip"</TD
</TR
</TABLE
<INPUT TYPE="BUTTON" VALUE="Download document"
onClick="sendForm(document.testform)"
</FORM
</BODY
</HTML
下面再给出需要用到的doc.html文件:
doc.html
<HTML
<BODY
<H3This is the document</H3
</BODY
</HTML
为了测试一下这个功能,你可以打开cookie1.html并点击中按钮,你将被带到表单网页。如果你回到cookie1.html文件并点击按钮你就回直接连到文档去。