Reloading page in IE 7 clears text from Input fields
I have a web page,with a text input which a user will enter some text. There is a hidden input element on the page that stores the values submitted,so as a u开发者_如何转开发ser keeps entering values , they will be appended to the hidden input element, I then use the value stored here to output all values entered to the user, see the code below
<SCRIPT TYPE="text/javascript">
function get_val(val)
{
var name = val.input1.value;
if(document.getElementById("input2").value != "")
{
var new_name = document.getElementById("input2").value;
name = new_name.concat(" AND " +document.getElementById("input1").value);
document.getElementById("input2").value = name
alert(name);
history.go(0); //this will be explained below
}
if(document.getElementById("input2").value == "")
{
document.getElementById("input2").value = name;
}
}
</SCRIPT>
<h3>Enter word</h3>
<form id="frm1" name="frm1">
<input id="input1" name="input1" type="text" size="40" value="" />
<input id="input2" name="input2" type="hidden" size="40" value=""/>
<input id="button" name="button" type="button" value="get name" OnClick="get_val(this.form)"/>
</form>
Now note the history.go(0); line, I've put this in to demonstrate my problem. it works fine in most browsers, except sometimes in IE version 7.0.5730.1.3, when the page is reloaded, the hidden input element is cleared, causing undesirable results.
Does anyone know any reason why this occurs occasionally with Internet Explorer 7?
I'm not sure why IE7 behaves that way...but I don't like the idea of relying on any browser to save form field's value consistently or reliably.
Store the values in a cookie instead of a hidden element.
http://www.quirksmode.org/js/cookies.html
I think this is more like a feature of browsers, you shouldn't depend on. Rather use cookies or session variables to keep the fields.
精彩评论