views:

1035

answers:

6

The following line is apparently written best in dot notation. I am trying to clean my JavaScript code to make it strict. What does it mean?

if (ie||ns6)
{
    var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";
}

I added some context to my line of code, in case this helps? I know nothing about DOM. I am not trying to support Internet Explorer 4, this is not my code and I wouldn't be able to write JavaScript myself. I am only trying to get it compliant and the JSLint tool says about this line:

Problem at line 17 character 43: ['dhtmltooltip'] is better written in dot notation.

+1  A: 

The following seems to be more user-friendly.

var tipobj;
if (document.all)
    tipobj = document.all["dhtmltooltip"];
else if (document.getElementById)
    tipobj = document.getElementById("dhtmltooltip");
else
    tipobj = "";
Li0liQ
Perhaps, but it still uses square bracket notation, so doesn't answer the question.
David Dorward
You can change `document.all["dhtmltooltip"]` to `document.all.dhtmltooltip` if you like.
Li0liQ
You can, and that is the core of what the question is about.
David Dorward
Yep, thanks a lot!
skarama
A: 

It's using capability checking to retrieve an element with the id dhtmltooltip and falling back to an empty String if there is not a capability for doing the retrieval.

UPDATE: As others have pointed out, the check for getElementById should be first, and could probably be omitted since any browser that could be called "modern" with a straight face has had it for a long time.

UPDATE 2: With the new context, JSLint is complaining that it isn't document.all.dhtmltooltip. You should probably just rewrite the whole thing as:

var tipobj = document.getElementById("dhtmltooltip");

and be done with it.

Hank Gay
What does that have to do with using dot notation?
David Dorward
So this line would replace my entire line, make it lighter and more current but still work the same?
skarama
As long as you don't care about supporting IE 4 and other incredibly ancient browsers.
Hank Gay
Hehe, not at all, thanks a lot Hank!
skarama
You're welcome.
Hank Gay
A: 

A quick Google search says that document.all is only used to support IE4. It is an array that allows the browser to access different parts of the DOM (see here.)

The code you posted first checks if document.all exists. If not, it sets tipobj to "". Now, beyond this, it's not really worth deciphering the line you posted unless you really want IE4 support. Since very few people still use IE4 and this piece of code isn't compliant to any modern standards, I'd just drop that line and set tipobj to "".

avpx
What does that have to do with using dot notation?
David Dorward
+5  A: 

There are two ways to access properties of an object in JavaScript.

Dot notation

foo.bar.baz

Square bracket notation

foo['bar']['baz']

You are using the latter in part of your code.

Douglas Crockford, who wrote JSLint (a tool which gives that error message), is of the opinion that is is better to use dot notation where possible.

David Dorward
Thank you, that explains it! You seem to consider his opinion not necessarily 100% valid tho, is there a better way then JSLint to validate my code?
skarama
I don't think there is a better way to validate the JS code, other than running it and testing it. I think the point @David Dorward was trying to make is that both are valid, and it is just a matter of style. The dot notation is the more preferred style, by most people, but there is nothing inherently wrong with using the square bracket notation.
pkaeding
jslint is basically the only good option. see http://stackoverflow.com/questions/331326/javascript-source-code-analyzer
chills42
skarama
DN is slightly more efficient than SBN, and is usually more readable. There are situations where I think that SBN is more readable, but that probably has more to do with the conventions I am used to than anything else. JSLint is a decent tool, don't take the wording of my answer as a criticism of it, my aim was to avoid promoting or disparaging it.
David Dorward
A markup validator will pick up issues related to expressing JavaScript in an HTML document. JSLint will not, it looks only at the JS, not the representation of it in a document.
David Dorward
Should I always call my js scripts externally instead of writing them straight in the html document?
skarama
Always? No, sometimes it is more efficient to include them directly. Usually, however, it is a better idea to keep them external and let them be cached.
David Dorward
@David-Dorward : "it looks only at the JS, not the representation of it in a document", that's not entirely true. You can put a whole HTML document into JSLINT and it will check some things that relate specifically to that, such as the inclusion of comment tags around the script, and properly escaping forward slashes in strings, to avoid confusing browsers that are looking for </script> to end the script.
Breton
@David-Dorward: It will also reprimand you for including any type or version attributes in the script tag.
Breton
+4  A: 

JSLint wants this:

var tipobj= document.all ? document.all.dhtmltooltip
                         : document.getElementById 
                           ? document.getElementById("dhtmltooltip") 
                           : "";

But nowadays is completely safe to assume that document.getElementById exists, it was introduced on the DOM Level Core 2 as of year 2000.

document.all is dead, unless you try to support really old browsers like IE4 (12 year old!):

var tipobj = document.getElementById("dhtmltooltip");

The two above snippets are a good example about the complexity cost of supporting very old browser versions:

alt text

CMS
Does this line replace mine entirely?
skarama
Yes, unless you want to support IE 4 or older.
CMS
Thank you kind sir!
skarama
A: 

It looks like the only real issues are formatting/syntax. This should work exactly the same and conform to javascript best practice. The main difference is using javascript dot notation instead of bracket notation.

if (ie || ns6) {
    var tipobj = document.all ? document.all.dhtmltooltip : document.getElementById ? document.getElementById("dhtmltooltip") : "";
}
chills42