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

Started by
6 comments, last by deadimp 16 years, 11 months ago
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]
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
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'>?
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 (&#106avascript, 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.<br><br>
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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
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.

Thanks for the advice.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Quote:Original post by deadimp
Plus, 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();}
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).
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement