I am a new user of Javascript. I am trying to use the script for browser langauge detection. I would like the browser to detect whether the user is English or French, How can I make the changes in the following script to do that? If you have a suggestions for another script that would be aprecaited too.
//*************** GLOBALS VARS *****************
//******************* BEHAVIOR FUNCTION **********************
// NSJ - 0,1,2: where to go if Netscape 2.0 or above Japanese
// NSnoJ - 0,1,2: where to go it NOT Netscape 2.0 or above Japanese
// IEJ - 0,1,2: where to go if IE 4.0 or above Japanese
// IEnoJ - 0,1,2: where to go if NOT IE 4.0 or above Japanese
// OTHER - 0,1,2: where to go if other browser
// eURL - English URL, often a filename, URL encoded. (ex: file.htm, // jURL - Japanese URL, often a filename, URL encoded. (ex: file.htm,
function checkLanguage(NSJ,NSnoJ,IEJ,IEnoJ,OTHER,eURL,jURL) {
//default to English
var newURL = eURL;
var version = parseFloat(navigator.appVersion);
//first check for Netscape 2.0 or up
if ((navigator.appName.indexOf('Netscape') != -1) && (version >= 2.0)) {
//check for Japanese
if (navigator.appVersion.indexOf('ja') != -1) {
newURL=jURL;
}
//check for IE 4.0 or up
} else if ((navigator.appName.indexOf('Microsoft') != -1) && (version >=4.0)) {
//check for Japanese
if (navigator.browserLanguage.indexOf('ja') != -1) {
newURL=jURL;
}
}
//make it so
if (newURL) {
window.location = unescape(newURL);
document.returnValue = false;
}
}
//******************* API **********************
//Can be used with any tag and any event
function canAcceptBehavior(){
return true;
}
//Returns a Javascript function to be inserted in HTML head with script tags.
function behaviorFunction(){
return "checkLanguage";
}
//Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
//Calls escape to encode URLs. Checks validity of all entries.
//For example, if *any* condition uses URL, the URL field cannot be empty.
function applyBehavior() {
var NSJ, NSnoJ, IEJ, IEnoJ;
var eURL, jURL, OTHER;
with (document.theForm) { // read all values from form
NSJ = NSJp.selectedIndex;
NSnoJ = NSnoJp.selectedIndex;
IEJ = IEJp.selectedIndex;
IEnoJ = IEnoJp.selectedIndex;
OTHER = OTHERS.selectedIndex;
eURL = escape(engURL.value); //URL encode
jURL = escape(jpnURL.value); //URL encode
}
//Check for valid numbers, and URLs exist if required
if ((eURL=="" && (NSJ==1 || NSnoJ==1 || IEJ==1 || IEnoJ==1 || OTHER==1))
return MSG_NoEURL;
if ((jURL=="" && (NSJ==2 || NSnoJ==2 || IEJ==2 || IEnoJ==2 || OTHER==2))
return MSG_NoJURL;
return "checkLanguage("+NSJ+","+NSnoJ+","+IEJ+","+IEnoJ+","+OTHER+",'"+eURL+"','"+jURL+"')";
}
//Returns a dummy function call to inform Dreamweaver the type of certain behavior
//call arguments. This information is used by DW to fixup behavior args when the
//document is moved or changed.
//
//It is passed an actual function call string generated by applyBehavior(), which
//may have a variable list of arguments, and this should return a matching mask.
//
//The return values are:
// URL : argument could be a file path, which DW will update during Save As...
// NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
// IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
// other...: argument is ignored
function identifyBehaviorArguments(fnCallStr) {
var argArray;
argArray = extractArgs(fnCallStr);
if (argArray.length == 7) {
return "other,other,other,other,other,URL,URL";
} else {
return "";
}
}
//Passed the function call above, takes prior arguments and reloads the UI.
//Converts strings into numbers for list indexes, and decodes URLs.
function inspectBehavior(argStr){
var argArray = extractArgs(argStr);
if (argArray.length == 7) { //function name plus 6 args
with (document.theForm) { // set all form values from args
NSJ.selectedIndex = parseInt(argArray[1]);
NSnoJ.selectedIndex = parseInt(argArray[2]);
IEJ.selectedIndex = parseInt(argArray[3]);
IEnoJ.selectedIndex = parseInt(argArray[4]);
OTHER.selectedIndex = parseInt(argArray[5]);
eURL.value = unescape(argArray[6]);
jURL.value = unescape(argArray[7]);
}
}
}
//**************** LOCAL FUNCTIONS ****************
function initializeUI() {
//not much to do here
}
Sawsie
//*************** GLOBALS VARS *****************
//******************* BEHAVIOR FUNCTION **********************
// NSJ - 0,1,2: where to go if Netscape 2.0 or above Japanese
// NSnoJ - 0,1,2: where to go it NOT Netscape 2.0 or above Japanese
// IEJ - 0,1,2: where to go if IE 4.0 or above Japanese
// IEnoJ - 0,1,2: where to go if NOT IE 4.0 or above Japanese
// OTHER - 0,1,2: where to go if other browser
// eURL - English URL, often a filename, URL encoded. (ex: file.htm, // jURL - Japanese URL, often a filename, URL encoded. (ex: file.htm,
function checkLanguage(NSJ,NSnoJ,IEJ,IEnoJ,OTHER,eURL,jURL) {
//default to English
var newURL = eURL;
var version = parseFloat(navigator.appVersion);
//first check for Netscape 2.0 or up
if ((navigator.appName.indexOf('Netscape') != -1) && (version >= 2.0)) {
//check for Japanese
if (navigator.appVersion.indexOf('ja') != -1) {
newURL=jURL;
}
//check for IE 4.0 or up
} else if ((navigator.appName.indexOf('Microsoft') != -1) && (version >=4.0)) {
//check for Japanese
if (navigator.browserLanguage.indexOf('ja') != -1) {
newURL=jURL;
}
}
//make it so
if (newURL) {
window.location = unescape(newURL);
document.returnValue = false;
}
}
//******************* API **********************
//Can be used with any tag and any event
function canAcceptBehavior(){
return true;
}
//Returns a Javascript function to be inserted in HTML head with script tags.
function behaviorFunction(){
return "checkLanguage";
}
//Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
//Calls escape to encode URLs. Checks validity of all entries.
//For example, if *any* condition uses URL, the URL field cannot be empty.
function applyBehavior() {
var NSJ, NSnoJ, IEJ, IEnoJ;
var eURL, jURL, OTHER;
with (document.theForm) { // read all values from form
NSJ = NSJp.selectedIndex;
NSnoJ = NSnoJp.selectedIndex;
IEJ = IEJp.selectedIndex;
IEnoJ = IEnoJp.selectedIndex;
OTHER = OTHERS.selectedIndex;
eURL = escape(engURL.value); //URL encode
jURL = escape(jpnURL.value); //URL encode
}
//Check for valid numbers, and URLs exist if required
if ((eURL=="" && (NSJ==1 || NSnoJ==1 || IEJ==1 || IEnoJ==1 || OTHER==1))
return MSG_NoEURL;
if ((jURL=="" && (NSJ==2 || NSnoJ==2 || IEJ==2 || IEnoJ==2 || OTHER==2))
return MSG_NoJURL;
return "checkLanguage("+NSJ+","+NSnoJ+","+IEJ+","+IEnoJ+","+OTHER+",'"+eURL+"','"+jURL+"')";
}
//Returns a dummy function call to inform Dreamweaver the type of certain behavior
//call arguments. This information is used by DW to fixup behavior args when the
//document is moved or changed.
//
//It is passed an actual function call string generated by applyBehavior(), which
//may have a variable list of arguments, and this should return a matching mask.
//
//The return values are:
// URL : argument could be a file path, which DW will update during Save As...
// NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
// IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
// other...: argument is ignored
function identifyBehaviorArguments(fnCallStr) {
var argArray;
argArray = extractArgs(fnCallStr);
if (argArray.length == 7) {
return "other,other,other,other,other,URL,URL";
} else {
return "";
}
}
//Passed the function call above, takes prior arguments and reloads the UI.
//Converts strings into numbers for list indexes, and decodes URLs.
function inspectBehavior(argStr){
var argArray = extractArgs(argStr);
if (argArray.length == 7) { //function name plus 6 args
with (document.theForm) { // set all form values from args
NSJ.selectedIndex = parseInt(argArray[1]);
NSnoJ.selectedIndex = parseInt(argArray[2]);
IEJ.selectedIndex = parseInt(argArray[3]);
IEnoJ.selectedIndex = parseInt(argArray[4]);
OTHER.selectedIndex = parseInt(argArray[5]);
eURL.value = unescape(argArray[6]);
jURL.value = unescape(argArray[7]);
}
}
}
//**************** LOCAL FUNCTIONS ****************
function initializeUI() {
//not much to do here
}
Sawsie