hey Shane,

Nick's comments above simply reinforce the need for explicit variable
declaration. ignoring the "var" keyword, even when picking names that
don't collide with properties of the window object, is a dangerous
practice. it leads to hard-to-spot bugs, scope issues, and a whole
mess of other problems.

furthermore, implicit assignment (ignoring the "var" keyword to scope
a variable globally) has some quirks in relation to overwriting
properties of the window. from an article by kangax (http://
thinkweb2.com/projects/prototype/detecting-global-variable-leaks/):

"To be more precise, undeclared assignment actually results in global
property assignment, not global variable declaration. The difference
between two is rather subtle: variable declaration creates non-
deletable property of a global object, whereas explicit or implicit
property assignment creates deletable one. Another peculiarity can be
observed in IE, where global property assignment is disallowed if
there's an element in a document with the same-named ID or NAME value.
Global variable declaration, on the other hand, quietly overwrites
existing property in cases like this. "

Effectively, outside of any other code, the statements:

top = "someValue";
var top = "someValue";

both produce a globally scoped variable named "top" with a value of
"someValue". in the first example, however, because implicit
assignment is used AND "top" is an existing property of the global
object (window in the case of the browser) IE chokes. there is a
wonderful solution that couldn't be easier to remember:

don't omit the "var" keyword. ever. :)

hope that helps!



On Sep 24, 6:58 am, Nick Fitzsimons <n...@nickfitz.co.uk> wrote:
> 2009/9/24 Shane Riley <shanerileydoti...@gmail.com>:
>
>
>
> > Weird double post. Found the issue. For some reason declaring the
> > variable worked. So I changed it to:
> > var  top = (parseInt($(this).height()) / 2) - 6;
>
> That's because "top" is already defined as a synonym for the top-level
> window object, and IE gets upset if you try to overwrite it.
>
> I think you'd get the same "Not implemented" error if you tried to
> assign values to the global "self", "parent", and "window" properties
> too. Assuming you're not running in a frame or an iframe, "self",
> "top", "parent", and "window" all refer to the same object. In a frame
> or iframe, "self" and "window" will refer to the framed document's
> "window", and "top" will refer to the "window" object of the very
> topmost document, while "parent" will refer to the "window" object of
> the document containing the frame, which is not the same as "top" if
> you have nested frames/iframes.
>
> Regards,
>
> Nick.
> --
> Nick Fitzsimonshttp://www.nickfitz.co.uk/

Reply via email to