Stop alert message from poping up when back button is clicked.
I'm using VB .Net 2003. I have a web page where an alert button pops up if there is an error. If the user changes their criteria and clicks the submit button again then a report is created that they view. If they hit the back button, the old alert button pops up again. How can I stop this alert button from popping up again when they hit the back button?
Thanks
Re: Stop alert message from poping up when back button is clicked.
Re: Stop alert message from poping up when back button is clicked.
You can use javascript to remove the script if you tag it with an ID.
Code:
<script id="popAlert" type="text/javascript">
alert("Error!");
</script>
Code:
function RemoveAlert()
{
var head = document.getElementByTagName("head");
var script = document.getElementsByTagName('script');
if (script.id == "popAlert")
head[0].removeChild(script[i]);
}
Re: Stop alert message from poping up when back button is clicked.
Nevermind... that won't work for history as the browser retains the document as it was.
One option then is to use Cookies. This checks to see if the cookie is there, if it is not (the first time), it pops the alert. Now, anytime after that, it won't show the alert. It of course set the cookie to expire in one day, so will have to make adjustments server-side to erase the cookie in the Response stream if they have an error.
Accessing cookies through javascript can raise alerts in browsers depending on the client's security settings.
Option two, you may have better results by sending a NO-CACHE tag to the client - which could perhaps destroy history (never tried it myself).
Your other option is to intercept the user's back and redirect them to the current document again (history.go(0) i believe).
Code:
<script type="text/javascript">
if (getCookie('srage') == null)
{
alert("Error!");
setCookie('srage','1','0');
}
function getCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{ begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;
}
function setCookie(NameOfCookie, value, expiredays)
{ var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
function delCookie (NameOfCookie)
{ if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}</script>
Re: Stop alert message from poping up when back button is clicked.
Pls tell me where I should give this code and how to call the alert
Re: Stop alert message from poping up when back button is clicked.
Pls tel me where I should give this code and how to call the alert
Re: Stop alert message from poping up when back button is clicked.
You could just tell the browser to not cache the page.