[Javascript] problem with swfobject.js

Nick Fitzsimons nick at nickfitz.co.uk
Mon May 15 20:57:00 CDT 2006


Schalk wrote:
> I am using swfobject.js to include flash on a website. Both IE and 
> Firefox complains about the following snippet of code:
> 
> if(this.skipDetect||this.getAttribute("doExpressInstall")||this.installedVer.versionIsValid(this.getAttribute("version"))){ 
> 
> var n=(typeof _20=="string")?document.getElementById(_20):_20;
> n.innerHTML=this.getSWFHTML();
> return true;
> }else{
> if(this.getAttribute("redirectUrl")!=""){document.location.replace(this.getAttribute("redirectUrl"));}} 
> 
> return false;}};
> 
> In particular, it complains about the 'n' variable stating that it has 
> no properties in FF and IE says Object expected. Has anyone else come 
> across this problem? Any ideas on how to fix this? Thanks!
> 

Well, I don't know the code you're using, but I know those errors. They 
turn up because the variable named n (in this case) is being assigned a 
reference to something that isn't there - which of course means that 
it's referring to nothing at all. As whatever it's referring to is 
non-existent (null, in this case), that thing has no properties; when 
you try to access the properties of a non-existent object via the 
property access operator ".", Firefox tells you the thing before the 
property access operator "has no properties" (because it's null), and IE 
tells you that it "expected" an "object" (rather than null) whose 
properties it might access.

The usual cause of something like this is that you have assigned a 
reference to the variable named "n" but what you've assigned is 
something that doesn't actually exist in the page. In this case the code 
is assigning the value to the variable "n" that is the result of the 
ternary expression:

(typeof _20=="string")?document.getElementById(_20):_20

This appears to be an unholy mess of code, but that's not the point; 
let's just say that code written with an IE4 (1997) mindset, and then 
patched to try to make it work properly on later and better browsers, is 
often written very badly. It doesn't work in your document because the 
document contains no element (by which I mean the DOM Level 1 definition 
of an Element) which has its "id" attribute set to the value "_20". I 
can say this with absolute certainty even though I haven't seen the 
document, because if the document contained an element with the id "_20" 
then that element would be assigned to the variable "n" and would have a 
(non-standard) innerHTML property, and the code would work. If you had 
several elements which (incorrectly) all had the id "_20" then the 
assignment would have produced a NodeSet rather than null, and the error 
would be all about "does not support this property or method" rather 
than "has no properties". It's worth knowing about these differences in 
the kinds of errors produced in the different cases as, although to a 
cursory glance they appear as opaque as each other, there actually is 
useful information in those messages.

As an aside, I would suggest that you be cautious about using code that 
has abysmal naming conventions; it often causes more problems than it 
solves. Good naming is a crucial part of good coding, and I can't think 
of anything at all for which "_20" would be a good name. (Well, apart 
from the newborn child of a Hollywood celebrity who's trying to get some 
headlines.)

Check your document and ensure that it has a relevant element with the 
id attribute set to "_20" and it may well work - although it's a wipeout 
for anybody with JS disabled or no Flash plugin. But you knew that :-)

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/





More information about the Javascript mailing list