Click to See Complete Forum and Search --> : Is there a way


Jona
01-25-2003, 09:19 PM
to make an onMouseOver alert() only alert once? Like:

<a href="somewhere.com" onMouseOver="alert('haha')">somewhere.com</a>

And it only alerts once?

slynx
01-25-2003, 10:29 PM
Have the onMouseOver call a function that has the alert in an if statement that checks a flag variable (set to 0 outside the function. Set the flag to one in a statement after the alert.

Charles
01-26-2003, 06:03 AM
<a href="http://www.w3.org/" onmouseover="alert(); this.onmouseover = function () {}">W3C</a>

azcn2503
01-26-2003, 06:35 AM
Don't know if this will work....


<html>
<head>
<script language="javascript">
alerted=0;
function alerter(text){
if (alerted==0){
window.alert(text);
}
if (alerted==1){
// no action performed
}
alerted=1;
}
</script>
</head>
<body>
<a href="#" onMouseOver="alerter('HAHAHA!');">Ha!</a>
</body>
</html>

khalidali63
01-26-2003, 07:35 AM
Originally posted by azcn2503
Don't know if this will work....


......Just curious,why post it if you don't know its going to work or not??????

cheers

Khalid

azcn2503
01-26-2003, 07:43 AM
It does work though :P

I thought I may as well have a go.

Jona
01-26-2003, 04:18 PM
Thanks guys, I was just curious.. :)

Jona
01-26-2003, 04:26 PM
Charles, why do you always use the, "var = function() { }"?

I mean, it's shorter than the way I code, but mine (to me anyway) has more logic. Just wondering :) I think I'm always too curious lol.

Charles
01-27-2003, 06:26 AM
To me that use of anonymous functions is much more logical. And it is consistent with the object oriented nature of JavaScript. See my post towards the end of http://forums.webdeveloper.com/showthread.php?s=&threadid=2852. Also, you need to keep in mind that each line of code in JavaScript carries a great deal of overhead. I always try to keep things as short as possible and I never use a variable if I can avoid it.

But let's consider your example. With the other method proposed, each time the link's onmouseover handler is called, the interpreter has to look up the variable name, get its value, compare it and decide what to do. And at each of those steps a good deal is going on unseen. With my method, the first time the handler is called the alert is called and then the handler overwrites itself. With each subsequent call the handler simply does nothing. Call me nuts, but that seems to make more sense to me.

Jona
01-27-2003, 04:20 PM
On the contrary, it makes very good since, Charles. Very good sense. As a matter of fact, I like the way you "code." The shorter the better--especially for the people with the old Windows 3.1 with a 16K modem! Hehe, I don't even think they'll be able to view it! That version of JavaScript wasn't released when I first got online with my 3.1.. Anyways, back to our discussion--your code is very well put together. It is, at first glance, a bit odd, but when explained, makes very good sense. There seems to be more logic in azcn2053's code to me, but I understand variables, loops, etc., which is what he used. In your code, I am unfamiliar with it. Both work, at least on a Windows Me with 360+ MB memory.. Well, I hope to speak with you again, Charles. Your method of coding is very interesting, and I look forward to seeing your other ways of getting around the "long" functions... personally, I always make very short code. Many DHTML scripts are rather lengthy, but I always try to shorten them up a little, because I know what it's like to have a 28.8 and 56K modem.

Charles
01-27-2003, 07:24 PM
If you are wed to the setting a flag method and you still want to keep your code as elegent as possible then try:

<a href="http://www.w3.org/" onmouseover="if (!window.doneThat) {alert(); window.doneThat = true}">W3C</a>

Jona
01-27-2003, 07:29 PM
Now that makes sense off the top of my head. ;)

These little "tid-bits" will really help me later on in life :)