如何强制浏览器不缓存文本字段?

时间:2014-04-20 04:36:07

标签: html input client client-side textfield

考虑登录HTML页面:

<%@ page language="java" 
    contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head><title>Bank application</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>

<link rel="stylesheet"
      href="./css/forgotPass.css"
      type="text/css"/>      
</head>

<body>
<table class="title">
  <tr><th>Web Bank application</th></tr>
</table>
<br/>


<!-- JS Code to make sure that the user MUST enter something in the login page -->
<script>
function verifyEmptyString()
{
    var username = document.forms["loginForm"]["username"].value;
    var password = document.forms["loginForm"]["password"].value;

    if (username == null || username == '' || password == null || password == '')
    {
        alert("Both Username and Password are required !");
        return false;
    }

    return true;
}     
</script>


<fieldset>
  <legend>Login Page - please enter your Username and Password</legend>

  <form onsubmit="return verifyEmptyString(this)" id="loginForm" action="loginPage" method="post" > 
  <!-- note we use here a paragraph & font size -->
  <!-- Notice we use a Required field !!! -->

    <p style="font-size:15px"><span style="color:red;font-weight:bold;">*</span> Username: <input type="text" name="username" value="" ><br> </p>
    <p style="font-size:15px"><span style="color:red;font-weight:bold;">*</span>  Password : <input type="password" name="password"><br> </p>
    <input type="submit" value="Login">

    <br> <br>
    <a href="#" class="myButton">Forgot your password ?</a>
  </form>
</fieldset>

<br/>
<br/>
<br/>
<br/>
<br/><br/><br/><br/><br/><br/>
</body></html>

Username的文本字段始终被缓存。

当我使用时:

<input type="text" name="Username" autocomplete="off">

然后我得到:未定义的属性名称(自动填充)

并且所有浏览器都不支持。

如何在所有浏览器中完全强制文本字段不记住输入的先前值?

1 个答案:

答案 0 :(得分:1)

将此添加到 HTML

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />