SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Help with Browser Detect Error

    Okay, I'm less than fluent with JavaScript (I can do an alert and a pop-up window!). But I've managed to debug my problem. I didn't write this, I'm just trying to fix it. In the code below, the if statement checks to see if the browser is Netscape 4 and if so, subtract one from the users age becase the math is always one too high. Here is what I've determined.

    IE6 Gets detected as Netscape 4 and does the subtraction making it 1 too low.

    IE5x gets detected correctly and the script returns the correct age.

    Netscape 4X Returns a Year too high

    Netscape 6x Returns the correct age.

    Below is the entire function. I've commented the lines that are causing the problem. It's just below this:
    ERROR : The Below IF logic is where the ERROR is


    function finalizeDOB(form)
    {
    var oError = new Error(); // Error object for date validation
    var dtToday = new Date(); // Todays date
    var nThisYear = dtToday.getFullYear(); // The current year

    // Make sure the user only entered digits
    if ( ! isNumeric(form.Year.value) )
    {
    alert("Invalid character in the Year.");
    form.Year.focus();
    return (false);
    }

    // If year is two digits prepend "19" on front
    if ( form.Year.value.length == 2 )
    {
    form.Year.value = "19" + form.Year.value;
    }

    // At this point insure that we have a four digit year.
    if ( form.Year.value.length != 4 )
    {
    alert("Please check the year value, it must be either 2 or 4 digits.");
    form.Year.focus();
    return (false);
    }

    // Insure that the year is within 1890 and now
    if ( form.Year.value >= 1890 && form.Year.value <= nThisYear )
    {
    // Set the hidden DOB field value

    form.txtDOB.value = form.Month.selectedIndex + "/" +
    form.Day.selectedIndex + "/" + form.Year.value;

    // Make sure the date is valid
    if ( isDate(form.txtDOB, oError ) )
    {
    var dtDOB = new Date(form.Year.value, form.Month.value, form.Day.value);
    var dtDiff = dtToday.getTime() - dtDOB.getTime();
    form.txtAge.value = Math.floor(dtDiff /(1000 * 60 * 60 * 24 * 365.25));

    //Netscape 4.x is always 1 to high
    // ERROR : The Below IF logic is where the ERROR is
    if ( String(form.txtBrowser.value).match("Netscape") &&
    String(form.txtBrowserVersion.value).charAt(0) == "4" )
    {
    form.txtAge.value = form.txtAge.value - 1;
    }

    return (true); // We have a valid date
    }
    else
    {
    // Show date error and set focus to the appropriate control
    alert(oError.Description);
    if ( oError.Number == 1 )
    {
    form.Month.focus();
    }
    else
    {
    form.Day.focus();
    }

    form.txtDOB.value = "";
    return (false);
    }
    }
    else
    {
    alert("Please check the year value, it must be between 1890 and " + nThisYear);
    form.Year.focus();
    return (false);
    }

    }

    Any help would be much appreciated.
    Chrispian H. Burks
    Nothing To Say

  2. #2
    SitePoint Zealot Drew630's Avatar
    Join Date
    Nov 2001
    Location
    Maryland
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um....I don't quite know what you want, but try this on for size and see if it may be helpful in sovling your problem.

    Code:
    function browserType() {
    	var NS = 0; 
    	var IE = 0; 
    	var other = 0;
    	
    	// determine the browser type
    	if (navigator.appName.indexOf('Netscape') != -1) {
    	   NS = 1;  //navigator is browser
    	}
    	else {
    		 if (navigator.appName.indexOf('Microsoft Internet Explorer') != -1) {
    		 	IE = 1;  // IE is browser
    		 }
    		 else {
    		 	other = 1;  // Any other broswer
    		 }
    	}
    	browserVersion= parseInt(navigator.appVersion);
    	//Netscape 4
    	if (browserVersion < 5 && NS) {
    		  // decrease your age, since this is version 4.x of Netscape   	  
    	}			
    	// you can add if statements for all of the browsers that you check for
    	
    }
    It works to get the browser type and version number. you can add your own code to play with the age.

    Drew
    ~Drew

    There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.

  3. #3
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Drew630,

    Thanks a bunch, That worked perfectly.
    Chrispian H. Burks
    Nothing To Say


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •