I need some help in my html page

94 views Asked by At

Is there a way to know if the user just opened the page? I got a textbox in my app that will retain its value even if the page was refreshed or submitted. What I want to do is to remove the content of the box when the user just accessed the page because what's happening right now is that whenever I close and rerun the page, the value of the textbox before I closed it is still there. Can you help me out?

2

There are 2 answers

0
AudioBubble On BEST ANSWER

localStorage does it for you:

var element = $("input[type='text']");
var message = "This is the first time !";

if (localStorage.getItem("onlyOnce")) {
    element.val(""); // clear value
}
else {
    localStorage.setItem("onlyOnce", true);
    element.val(message);
    alert(message);
}

You can use together with the above:

$(window).on("beforeunload", function() { 
    localStorage.removeItem("onlyOnce");
});

Hope it helps.

6
Power Star On

You can add this script in document.ready function.

<script>
$(document).ready(function(){
$("input[type=text]").val("");
});
</script>