[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
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉