[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.


/**
 * I am using a JavaScript module to isolate the code of each snippet.
 * In fact it's a Vue.js mixin. Take the code called by the mounted()
 * or the snippetXX() function.
 */
export default {
  methods: {
    snippet24 () {
      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! 😁
    }
  },
  mounted () {
    if (this.isArticle(24)) {
      this.snippet24()
    }
  }
}

 More on Stackoverflow   Read the doc  Random snippet

  Work with me!