[thelist] Javascript question

Tom Dell'Aringa pixelmech at yahoo.com
Thu Aug 15 10:50:01 CDT 2002


See my comments below:

--- Sergey 'the Eych' Smirnov <eych at ageofweb.ru> wrote:
> Greetings,
>
> I have a problem passing certain parameter from one function to
> another in Javascript. Here is the story.
>
> I have a div on the page with id 'submenu'. On 'onmouseout' event i
> want to hide it. Also i want to wait for several hundred
> milliseconds
> before i do, so that the user will have time to point at it with a
> mouse
> (this is a simple dhtml popup menu).
>
> If i am right then the only way to ask the browser to wait is to
> use
> window.setTimeout("function()", millisecs) call.
>
> So i try the following:
>
> >> [code quote]
>
> function waitAndHide(id) {
>   timer = window.setTimeout('hideDiv('+id+')', 250);
> }
>
> function hideDiv(id) {
>   var element = getId(id);
>   if (element) {
>     (ns4) ? element.visibility = "hidden" :
> element.style.visibility = "hidden";
>   }
>   return false;
> }
>
> >> [/code quote]
>
> Now, id that is passed to waitAndHide is not an object - it is just
> a
> div id - a string. With getId() i am getting an object from 'id'
> and
> then i use it.
>
> But! For some strange reason id is not passed to hideDiv. It is
> defined and works ok inside waitAndHide() but it is undefined in
> hideDiv(). What might be the problem? What am i missing?
>
> Also, for another strange reason i found out that IE 6.0 thinks
> that
> 'id' is already an object inside waitAndHide and i can get id from
> it
> with 'id.id'. While Mozilla doesn't think that 'id' is an object
> and i
> just can't make it pass 'id' correctly to hideDiv.
>
> Any help will be appreciated.
>
> ---
> Sergey "the Eych" Smirnov
> One of the Stuffed Guys
> http://www.stuffedguys.com

I saw a couple of things. One way I work (I know some people don't
like it) is to get it working on one platform first, then add the
cross browser code (and I prefer object detection to browser
sniffing..thats another thread).

This works in IE. First the code:

--------------------
<html>
<head>
<script>

function waitAndHide(id) {
  timer = window.setTimeout("hideDiv('"+id+"')", 250);
}

function hideDiv(element) {
	var el = document.getElementById(element);
	el.style.visibility = "hidden";
}
</script>
<style>
#myDiv {visibility: visible;}
</style>
</head>

<body>
<div id="myDiv" onmouseout="waitAndHide(this.id)">Some stuff</div>
</body>
</html>
-------------------------------------


The first problem was in this line:

setTimeout("hideDiv('"+id+"')", 250);

you needed to quote the id to pass it as a string, but you only had

timer = window.setTimeout('hideDiv('+id+')', 250);

I believe since you only used single quotes all the way through the
expression, JS wasn't seeing the id as a string. Remember,
concatanation has to be done "outside" the expression as it were.
That's where color coding comes in helpful ;)

As someone else mentioned, you don't need the getID function. Just
pass the id using the this keyword (this.id) which works nicely.
Don't know what your target browsers are, so you may have to adjust
this or test it.

But you have something that works to start with :)

Tom


=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com



More information about the thelist mailing list