D
D
Dmitry Pavlov2015-11-04 23:40:07
JavaScript
Dmitry Pavlov, 2015-11-04 23:40:07

How can I make the alert only appear on the first page load?

How can I make the alert only appear on the first page load? So that when you go to other pages, and then back to the main one, alert does not appear.

<script>
function hello()
{
alert("Вітаю вас на головній сторінці!")
}
</script>
<body onLoad="hello()">

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Idel Gizatullin, 2015-11-04
@dmtreaqq

Write to localstorage, for example:
And check

if (localStorage.getItem("greeted") !== true)
  alert("Вітаю вас на головній сторінці!")

It is also possible to write down information about whether the greeting was shown in cookies. This will work more reliably in older browsers and the information can still be read from the server (sometimes this can come in handy).
FunctionsgetCookie(name) and setCookie(name, value, options)can be peeped in the article about cookies .

V
Vyacheslav, 2015-11-04
@vacslav-dev

Add an entry to the cookie, on the first visit, and check for its existence. If there is no record, we display a message and set the cookie, if there is, we do nothing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question