Link to home
Start Free TrialLog in
Avatar of sunny012097
sunny012097

asked on

How to have javascript alert run only once

Hello experts,

I am using registerclientscript in asp.net to pop up an elert message. It works fine. My problem is, when I browse to another page and click back on my browser that script runs again. I was wondering if there is a way to run the alert only once so that if a user clicks the back button of the browser they don't see the alert message again.

Thanks.
Avatar of basicinstinct
basicinstinct
Flag of Australia image

i'd say not
ASKER CERTIFIED SOLUTION
Avatar of numbers1to9
numbers1to9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of numbers1to9
numbers1to9

if(cookie==not visited) {alert('Hello handsome')]
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to delete a cookie if you don't need then call the deleteCookie function as

deleteCookie("isAlerted");

that's all
another simple solution is to use session variable to set whether the alert message is shown or not.
so whenever the user comes to this page, the session variable's value will be checked and the alert message is shown is not already shown.

this will be helpful when the user has disabled cookies.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually we can delete cookies by setting the expiry time. here in the example when the user closes the browser, the cookie will be automatically deleted. so when the user comes back to the page again, the alert Message will be shown, because the cookie will not be present.