SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I modify this simple script?

    I’m using this java script to apply different styles to form element depending on what browser the visitor is using. How can I change the script so that I can specify different styles for Netscape 4.x and Netscape 6.x?
    Code:
     <FORM>
        <SCRIPT>
          if (navigator.appName == 'Netscape') {
            document.write('<INPUT type="text" size=20>\n');
          } else {
            document.write('<INPUT type="text" size=30 class=”formstyle”>\n');
          }
        </SCRIPT>
        <NOSCRIPT>
          <INPUT type="text" size=20>
        </NOSCRIPT>
      </FORM>

  2. #2
    SitePoint Enthusiast jlgarcia's Avatar
    Join Date
    Sep 2001
    Location
    Baja California, México
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI, I don't know much about JS but I found this page for you in "How to Sniff the Browser Type":

    http://www.webreference.com/js/column76/3.html

    hope it helps you.
    Use the source, Luke...

  3. #3
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jlgarcia! This seems to work:

    Code:
      
    <FORM>
    <SCRIPT>
    var browser_type=navigator.appName
    var browser_version=parseInt(navigator.appVersion)    
    //if NS 6
    if (browser_type=="Netscape"&&browser_version>=5) {
    document.write('<INPUT type="text" size=15 class=”formstyle”>\n');
    //if NS4+
    } else if (browser_type=="Netscape"&&browser_version>=4) {
    document.write('<INPUT type="text" size=15>\n');
    } else {
    document.write('<INPUT type="text" size=30 class=”formstyle”>\n');
    }
    </SCRIPT>
    <NOSCRIPT>
    <INPUT type="text" size=20>
    </NOSCRIPT>
    </FORM>

  4. #4
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Marina
    Thanks jlgarcia! This seems to work:

    Code:
      
    <FORM>
    <SCRIPT>
    var browser_type=navigator.appName
    var browser_version=parseInt(navigator.appVersion)    
    //if NS 6
    if (browser_type=="Netscape"&&browser_version>=5) {
    document.write('<INPUT type="text" size=15 class=”formstyle”>\n');
    //if NS4+
    } else if (browser_type=="Netscape"&&browser_version>=4) {
    document.write('<INPUT type="text" size=15>\n');
    } else {
    document.write('<INPUT type="text" size=30 class=”formstyle”>\n');
    }
    </SCRIPT>
    <NOSCRIPT>
    <INPUT type="text" size=20>
    </NOSCRIPT>
    </FORM>
    Just to let you know you can also detect based on the dom of the browser for example

    if (document.getElementById){
    document.write('<INPUT type="text" size=15 class=”formstyle”>\n');
    }else if (document.layers){
    document.write('<INPUT type="text" size=15>\n');
    }else{
    document.write('<INPUT type="text" size=30 class=”formstyle”>\n');
    }

    I wasn't sure what the else was specifying but if it is there for ie and other new browsers the document.getElementById will contain those.

    Hope that helps
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  5. #5
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, the "else" is there for IE. Thank you Maelstrom!

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Marina,

    If you are truly interested in browser-detection via properties, see the code in
    this thread.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •