SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript browser recognition

    We have javascript password strength indicator that works in IE but not Firefox, This is the part that deals with the browser support, does anyone know what needs adding to support firefox:



    Code:
    <script language="javascript">
    var ieDOM = false, nsDOM = false;
    var stdDOM = document.getElementById; function initMethod()
    {
    //Determine the browser support for the DOM
    if( !stdDOM )
    
    {
    ieDOM = document.all;
    if( !ieDOM )
    
    {
    nsDOM = ((navigator.appName.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion) ==4));
    }
    }
    
    passwordChanged();
    }
    function getObject(objectId)
    
    {
    if (stdDOM) return document.getElementById(objectId);
    if (ieDOM) return document.all[objectId];
    if (nsDOM) return document.layers[objectId];
    }
    thanks for any help ....
    codeslaw

  2. #2
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see anything wrong with it, at first glance. Are there any messages in your Javascript console?

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are no error messages in the j/s console.

    What is suppose to happen is that when a customer enters a password there is a html indicator box below the password field and if the password is evaluated as weak then the weak box background becomes red. If the password is medium the weak box and the medium box background becomes yellow. If the password is strong then all 3 boxes are green.

    We just dont seem to know what the problem is whether it is the DOM thing or even something else.

    This is the full function just in case there any other problems visible?

    Code:
    <script language="javascript">
    var ieDOM = false, nsDOM = false;
    var stdDOM = document.getElementById; function initMethod()
    {
    //Determine the browser support for the DOM
    if( !stdDOM )
    
    {
    ieDOM = document.all;
    if( !ieDOM )
    
    {
    nsDOM = ((navigator.appName.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion) >=4));
    }
    }
    
    passwordChanged();
    }
    function getObject(objectId)
    
    {
    if (stdDOM) return document.getElementById(objectId);
    if (ieDOM) return document.all[objectId];
    if (nsDOM) return document.layers[objectId];
    }
    function getObjectStyle(objectId)
    
    {
    if (nsDOM) return getObject(objectId);
    var obj = getObject(objectId);
    
    return obj.style;
    }
    function showDefault(objectId)
    
    {
    showCell(objectId, "", "");
    }
    function showCell(objectId, foreColor, backColor)
    {
    getObjectStyle(objectId).color = foreColor;
    
    getObjectStyle(objectId).backgroundColor = backColor;
    }
    function showWeak()
    
    {
    showCell("pwWeak", "Black", "#ff3333");
    showDefault("pwMedium");
    
    showDefault("pwStrong");
    }
    function showMedium()
    
    {
    showCell("pwWeak", "Black", "#FFFF33");
    showCell("pwMedium", "Black", "#FFFF33");
    
    showDefault("pwStrong");
    }
    function showStrong()
    
    {
    showCell("pwWeak", "Black", "#33ff00");
    showCell("pwMedium", "Black", "#33FF00");
    showCell("pwStrong", "Black", "#33FF00");
    }
    function showUndetermined()
    
    {
    showDefault("pwWeak");
    showDefault("pwMedium");
    showDefault("pwStrong");
    }
    
    
    function passwordChanged()
    {
    var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
    var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    var enoughRegex = new RegExp("(?=.{6,}).*", "g");
    
    
    var pwd = getObject("password").value;
    if( false == enoughRegex.test(pwd) )
    {
    showUndetermined();
    }
    else if( strongRegex.test(pwd) )
    {
    showStrong();
    }
    else if( mediumRegex.test( pwd ) )
    {
    showMedium();
    }
    else
    {
    showWeak();
    }
    }
    </script>
    thanks

    codeslaw

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Don't use browser sensing. Your code is set up for version 4 browsers and gets confudsed with some more recent browsers. Also since no one uses IE4 any more and almost no one uses Netscape 4 any more you could just dump all of that browszer sensitive code and use the standard document.getElementById that all version 5+ browsers support.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, will give that a try .....

    thank you,

    codeslaw

  6. #6
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We removed ieDOM and nsDOM and all references to it but that has caused it to stop working in explorer now? Guess we need, and I'm asking for a bit more instruction on how to reconfigure the code to include all 5+ browsers using .getElementById. Any help greatly appreciated, thanks.

    codeslaw
    Last edited by codeslaw; Jun 30, 2006 at 12:16.


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
  •