[Javascript] Display a message on the first access of a user on your website

Published on 2019-05-08 • Modified on 2019-05-08

Here a very simple way to display something to the user the very first time it accesses your website. It uses the browser localstorage. Of course is this case it is quite brutal as the message is displayed even before the page is loaded. You should of course modify this code to be displayed only after a delay or not on the first page load but the second one. But you get it, it's easy to modify to your needs.


<script>
    // https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage (hit the doc button)
    if(localStorage.getItem('popState') !== 'shown') {
        localStorage.setItem('popState', 'shown');
        alert('You will see this message only once. To test and see it again, clear your browser local storage for this website.');
    }
    // That's it! 😁
</script>

 More on Stackoverflow   Read the doc