Prevent input value from resetting on refresh
<input id="persistent_text_field" value=""/>
In your JS
assuming you're using jQuery
$(document).on('ready',function(){
if(sessionStorage.getItem('last_entry')){
$("#persistent_text_field").val(sessionStorage.getItem('last_entry'));
}
$("#persistent_text_field").on("keypress",function(){
sessionStorage.setItem('last_entry',$(this).val());
});
});
EDIT:
Non jQuery Solution? Simple :)
<input id="persistent_text_field" value="" onkeypress="setStorage(this)"/>
In your JavaScript
file call customReset();
in your document load event handler function.
function customReset(){
if(sessionStorage.getItem('last_entry')){
var element = document.getElementById("presistent_text_field");
element.value = sessionStorage.getItem('last_entry');
}
}
function setStorage(element){
sessionStorage.setItem('last_entry',element.value);
}
Browser refresh behaviour
<input autocomplete="off">
Preventing Firefox from remembering the input value on refresh with a meta tag
For an input
tag there's the attribute autocomplete
you can set:
<input type="text" autocomplete="off" />
You can use autocomplete for a form
too.
How do I stop timer reset after page refresh
You need localStorage
or cookies
to store data and use even after page reload.
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
UPDATE
After updated question, here is how you need to store and get counter
variable from localStorage
when you declared counter variable:
var counter = localStorage.getItem("counter");
counter = counter ? counter : 5 ; // if in store then that value otherwise 5 as default
You can detect page reload/refresh using javascript onbeforeunload
event and same time store the counter value for next use.
window.onbeforeunload = function() {
localStorage.setItem("counter", counter);
};
UPDATE-2
Here is a working fiddle that you can use:
https://jsfiddle.net/wdar3yuq/
clear input fields on page refresh (Microsoft Edge)
First, thanks for the responses.
I had tried autocomplete='off'
(sorry I forgot to mention that), but it didn't seem to work.
Just before checking here for replies, though, I decided to try one other experiment. I added a button to my page which, when clicked, would call a function containing the JavaScript .value = ''
statement.
For whatever reason, that worked just fine; it emptied the value and cleared the field, which got me thinking that tying the "reset" to a specific HTML event might be the key. I tried a couple of things that didn't work, but finally I just replaced this:
document.getElementById('input_field').value = '';
. . . with this:
window.onload = function() {
document.getElementById('input_field').value = '';
}
. . . and that did it. Every time I refresh the page in either Firefox or Edge, the value is emptied and the field is cleared.
I'm not sure why that worked (like I said, I'm just getting started with this stuff), but there it is.
Related Topics
Create a Newline for Every X Number of Characters
How to Detect Overflow of React Component Without Reactdom
React Open Modal Window on Click in Another Component
Disabling a Button Within a Stateless Component - React
How to Add Spaces to for Loop Output in JavaScript
Javascript Add Class Active When Click a Link After Load Another Page
Barcode Scanner for Mobile Phone for Website in Form
Invalid Attempt to Spread Non-Iterable Instance
How to Implement Ping/Pong Request for Websocket Connection Alive in JavaScript
Multiply and Sum Input Values Created Dynamically in Jquery
How to Increment a Number After Every 1 Second Using JavaScript
React-Select:Get Default Value in React-Select
Innerhtml Inserts Only [Object Htmldivelement]
Reload Datatable After Ajax Success
Onchange Is Not Triggering in React Js
Display a Loading Icon While Images Loads
Button Not Working on Mobile Devices But Works on Pc Bootstrap