# [web] [SOVLED] [JavaScript - IE6] Scoping in callback?

## Recommended Posts

IE just pisses me the eff off. I don't get why the hell it doesn't function as it should. Then again, that would be expecting something from Microsoft, wouldn't it? Enough for that small rant, the problem: I don't get how scoping works for js in IE. I try and assign a global a variable, but when a function is called later on (as callback) IE seems to have already deleted the variable's value, and that's nowhere near what I want it to do. Firefox acts as supsected, but IE... no, it just can't. Example:
<div id='item'></div>
<a href='javascript:crap()'>Stuff</a>
<script language='javascript'><!--
function o(x) { return document.getElementById(x); }
out=o("item");
function crap() { out.innerHTML="Testing stuff"; }
crap();
//--></script>


The 'crap()' instruction in the script tag acts as it should, where 'out' is still in scope. But in IE, when I click on the link, IE throws its oh-so-informative error telling me that 'out' is null, and nothing much else. I could define 'out' in the callback function, but that ain't gonna cut when I have to actually reference to an object or what not - plus, I want to know what I'm doing here, if it's me. Oddly enough, though, any variables stored in a 'class' (function with 'this' and '[name]' variables defined) can be accessed by the functions. [Edited by - deadimp on May 7, 2007 8:19:58 PM]

##### Share on other sites
Works for me in IE 7. I don't know of any bugs in IE's scoping rules, but I don't see any bugs in your code either, so this is weird. Do you get the same problem if you use a different variable name? What about if you assign out from an onload event handler? Does it do the same thing if you use <a href='#' onclick='crap(); return false'>?

##### Share on other sites
Why bother with IE6 support these days, the browser is dead and shouldn't be disturbed.
It only got away with being as bad as it is because it was pre-installed on every god-darned computer and thus developers were forced to work around its quirks.
Today it isn't the dominant browser anymore and any actions supporting its continued existance should be considered a crime against humanity.

Just add a small script (javascript, or PHP/ASP) to detect the browser version and if it is Internet Explorer 6 or an older IE version you can tell the user to upgrade.

##### Share on other sites
Looks to me more like a race condition.

Using DOM objects in scripts which are ran while the DOM is loading is a bad idea. These DOM objects *MAY* not yet exist.

Try moving the code from an immediate block to a window.onload event handler.

It's guaranteed that all the DOM will exist when window.onload is called. Scripts executing during the building of the page on the other hand, can't assume this.

Mark

##### Share on other sites
markr >> I'm not sure it's this, because the 'crap()' call at the end of the script works just fine, meaning that it's able to reference to the DOM Object alright. I don't think setting 'onload' would change anything... Plus, the way I've structure the site, the page doesn't define the body tag... Yeah. I'll go ahead and check the onload way.

[A few minutes later...]
Well, I've just checked it on an individual html file, and it's working fine... which is weird. I'll go back and check the original script, but I'm not sure I'll have the time just yet.

The audience for the current site mainly consists of computer-illiterate school parents, either than or parents who simply don't care how to really use a computer (which is essentially the same thing), so I can't say what kind of browser they're using. I probably need to install some sort of sampling thing to record how many hits I get from IE 6.

##### Share on other sites
Quote:
 Original post by deadimpPlus, the way I've structure the site, the page doesn't define the body tag...
You can set the onload event handler from anywhere like this:
window.onload = function() {    out=o("item");    crap();}

##### Share on other sites
Ah, thanks. Still haven't gotten around to testing it. I'll go ahead and do that now (and then study for my test tomorrow in Stat).

##### Share on other sites
Well, ends up that I, again, was ignorant. In some of my other code, I had defined 'out' without making it local, therefore whenever that code was called (which it was, at the end of the page), it would overwrite 'out'. Odd thing is, it didn't do this in Firefox, 'cause it worked before I made the changes...

Thanks for all the help.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628275
• Total Posts
2981763

• 10
• 11
• 17
• 10
• 9