SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict caser85's Avatar
    Join Date
    May 2002
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    browser detect styles

    Can someone tell me what is wrong with the code below? It's supposed to change the size of a text box for Netscape, but isn't working.

    Code:
    <script language="JavaScript">
    {
       var browser = navigator.appName;
       var version = versionNumber();
    
       if (browser == "Netscape")
    
       {
       document.write("<input onFocus="this.value='';" onBlur="if (this.value=='') {this.value='Search For...';}" type="text" name="query" value="Search for..." size="11" maxlength="150" />");
    
       } else {
    
       document.write(" <input onFocus="this.value='';" onBlur="if (this.value=='') {this.value='Search For...';}" type="text" name="query" value="Search for..." size="15" maxlength="150" />");
    
       }
    
    </script>

  2. #2
    SitePoint Guru bronze trophy blufive's Avatar
    Join Date
    Mar 2002
    Location
    Manchester, UK
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var browser = navigator.appName;
    if (browser == "Netscape")
    1) Navigator.appname isn't the best thing to check if you want to sniff the browser using javascript.

    See http://www.mozilla.org/docs/web-deve...wser_type.html for some guidance. The code on that page may be complete overkill for you, but it ought to give you some ideas of what to look for, and how to look for it, for instance by using indexof() rather than ==.

    Code:
    document.write(" <input onFocus="this.value='';" (etc.)
    2) you'll need to escape the quote marks in that string, like this:
    Code:
    document.write("<input onFocus=\"this.value='';\" (etc.)
    3) While netscape 4.x rendered textboxes at radically widths to other browsers, version 6 and 7 do different things depending what rendering mode you trigger. You should be aware of this when trying to compensate for Netscape 4.x.

    Finally, don't forget that there's more than just IE and Netscape out there.

  3. #3
    SitePoint Addict caser85's Avatar
    Join Date
    May 2002
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help! For future reference, here is what I did:

    Code:
       var browser = navigator.appName;
       var version = parseInt(navigator.appVersion);
        
       if (browser == "Netscape" && version <5)
       {
        document.write("<input onFocus=\"this.value='';\"");
        document.write("onBlur=\"if (this.value=='') {this.value='Search For...';}\"");
        document.write("type=\"text\" name=\"query\" value=\"Search for...\" size=\"10\" maxlength=\"150\" align=\"left\"/>");
       } else { 
        document.write("<input onFocus=\"this.value='';\"");
        document.write("onBlur=\"if (this.value=='') {this.value='Search For...';}\"");
        document.write("type=\"text\" name=\"query\" value=\"Search for...\" size=\"15\" maxlength=\"150\" align=\"left\"/>");
       }
    I still have one questions though. How can I put this script in my attached .js file and run it as a function? I know how to attach a javascript file, but I don't know how to call it in the page as a function.


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
  •