PDA

View Full Version : very simple thing - alert limited to one only



a_design_interactive
12-17-2006, 03:52 AM
hi. i'm new to javascript (believe it or not) and want to know how to limit the amount of times something happens. specifically, i want to do an onmouseout event handler have an alert popup (is there a less cheezy way than the actual "alert" window, like something from the DOM Tooltip? if that's not too complex?). but i don't want the alert to popup any more than one time-- even if the user moves from the page and comes back to it (in the same visit at least-- it doesn't necessarily need to be a bonefide "cookie", unless that's the only way... 'course, i only know about server-side cookies / sessions (yes, i do know some php fairly well)

thank you for your help!! re: what i said about the tooltip-- really, the standard alert window is fine. (unless tooltips ain't too difficult) -- i just don't want to go copy pasting code again. now that i've caught on a bit here-- i want to learn by actively doing, instead of copy/ paste, so i'm going to have to take it somewhat slow.... why do i not know how to do this alert? well-- i've searched and searched again-- onmouseover and alert, event handlers, you name it-- no one explains in their how-to anything about limiting [the alert to once only] -- only how it's done [and done again, and again, and...ugh!]

Just Commentary here:
crazy as it may sound (what? this guy has commentary?), after 10 years of experience w/ html, i have only in the past couple of days really got a [beginning] grasp of the whole javascript thing.

having realized finally that the event handlers are not javascript, but HTML... (sort of the "female end" to javascripts "male end" i've been thinking, or to VBscripts-- or ... dirty girl!) everything seems suddenly to be making sense. i've programmed some js in the past, but-- really mostly copy paste... and totally reliant upon the original author or tutorial / how-to. but now, i am finally going to be able to think about how it might be used in my work, as part of the design process vs the afterthought suggested through the process of some forum discussion... ;)

jscheuer1
12-17-2006, 07:11 AM
Cookie code from:

http://www.quirksmode.org/js/cookies.html


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

function doAlert(txt){
if(!readCookie('theAlert'))
alert(txt);
createCookie('theAlert',txt);
}
</script>
</head>
<body>
<span onmouseover="doAlert('Hello World');">Mouse Over Here</span>
</body>
</html>

Notes: Creates a session only cookie that allows the alert to fire once onmouseover of the item. Will fire every time if user has cookies disabled (unusual). Will fire again in the same session if user clears cookies (also unusual but, less so). A session only cookie lasts until all instances of the browser are closed. A persistent cookie can be set for (in the below case, 10) days:


createCookie('theAlert',txt,10)

a_design_interactive
12-17-2006, 11:47 AM
thanks!. this probably would have fit well in the "i need a ___ script" section?
but i'm glad you explained about the cookies. i'm sure your detail here will offer clarity in the future-- once i get the hang of it.

thanks again!