Before I start, I'd like to point out that critism in this post may not necessarily be aimed at you, but more likely 'cult' beliefs that follow client-side scripting and Web authoring.

Originally Posted by
Yorixz
I made [...] a script that is *supposed* to only show a layer to someone with the language nl-NL (Dutch) and otherwise show another layer.
If you really want to show language-specific content, the best way would be to use content negotiation. This would serve different resources based upon the Accept-Language header sent by user agents. You can Google for more information. The Apache Server documentation is particularly good.
I'll skip the rest of the first post as it isn't really relevant now.
var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all||window.opera) ? true : false;
var w3c = (document.all && w3c) ? true : false;
This form of "detection" (all forms, actually) is flawed. IE is not the only user agent to support the all collection, nor is NN4 the only user agent to support the layers collection. The identifier, ns6 is a misnomer as many user agents will produce assign true, and I have no idea how w3c is useful as it will be false in every single scriptable user agent.
There is no need to determine what a user agent is. Your only concern should be what it supports. The functions below demonstrate this principle.
Code:
var getRef = isGenericObject(document.getElementById)
? function(id) {
return document.getElementById(id);
}
: function() {return null;};
function isFunction(object) {return 'function' == typeof object;}
function isGenericObject(object) {return isObject(object) || isFunction(object);}
function isObject(object) {return !!object && ('object' == typeof object);}
function isString(object) {return 'string' == typeof object;}
function setDisplay(element, value) {var style;
if(isString(element)) {element = getRef(element);}
if(isGenericObject(element) && isGenericObject(style = element.style)) {
style.display = value;
}
}
The assignment to getRef is fairly simple. First, the getElementById property is examined: if it is an object or a function (IE considers it an object - don't ask me why), it is assumed that the host supports this particular DOM method. A wrapper is then created that calls gEBI. If the method is not supported, the wrapper will always return null. Whilst it doesn't provide a reference, it does prevent errors from calling non-existent functions. This approach could be extended to include the all and layers collections, but the only user agents that need these fallbacks are generally considered obsolete. If you're unfortunate enough to have visitors with these browsers, by all means add support. If not, I wouldn't bother.
The isX functions are just for support. They aren't really worth any time.
Ths setDisplay function accepts either a string or an object as its first argument. If this argument is a string, it assumes an id and uses getRef to obtain an object reference. If an object reference is available and it provides a style object, the display property is changed. This mechanism will work in various user agents including IE4 (if an object is passed, or getRef is extended), but not NN4. The best approach for solving the latter issue is probably:
Code:
function setDisplay(element, value) {var style;
if(isString(element)) {element = getRef(element);}
if(isGenericObject(element)) {
if(isGenericObject(style = element.style)) {
style.display = value;
} else if(isString(element.visibility)) {
style.visibility = ('none' == value) ? 'hide' : 'show';
}
}
}
with an extension to getRef. In both versions, the use of isGenericObject may be excessive, with isObject being sufficient. I don't have access wide enough range of user agents to decide that, though.
As for determining the language client-side, Gecko-based user agents use the language property, not browserLanguage. Using the isString and isObject functions defined above:
Code:
function getLanguage() {
if(isObject(this.navigator)) {
if(isString(navigator.language)) {return navigator.language;}
if(isString(navigator.browserLanguage)) {return navigator.browserLanguage;}
}
return 'unknown';
}
Note that this doesn't necessarily reflect the language preferences of the user; both properties return the language version of the user agent itself. For example, a Welsh user might prefer Welsh pages but can only get hold of an English user agent. This, coupled with reliability of a server-side solution, is why the latter is preferred.
Code:
switch(getLanguage().substring(0, 2)) {
case 'nl': setDisplay('english', 'none'); break;
default: setDisplay('dutch', 'none');
}
Note that the code above reverses your logic. You shouldn't rely on a script to reveal information hidden by CSS. If the features required by the script are not available, the user will not be able to get that information. Instead, allow things to be visible initially, and hide them via a script.
Finally, you really shouldn't let Dreamweaver (or any WYSIWYG software) generate junk like those "layers". It's ugly, unnecessary bloat. I'd need a URL for an alternative suggestion.
Mike
Bookmarks