handling forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hamster

    handling forms

    How can I pass user's browser, OS and resolution information to the
    form element?

    I need to make radiobuttons to be checked by default according to the
    user's information.

    For example,
    if visitor's browser is IE I would like the radiobutton with
    value="Internet Explorer" to be checked by default.

    Thanks.

    Here is the part of the form:

    <html>
    <head>
    <script language="JavaS cript">

    var browserName = navigator.appNa me
    var browserVersion = navigator.appVe rsion
    var OS = navigator.userA gent
    var screenWidth = screen.width
    var screenHeight = screen.height


    document.write( "Browser: <b>"+ browserName + " ??" +
    browserVersion + "</b><br>")
    document.write( "OS: <b>" + OS + "</b><br>")
    document.write( "Screen resolution: <b>" + screenWidth + "
    </b>x<b> " + screenHeight + "</b><br>")

    </script>

    </head>
    <form name="form1" method="post" action="">
    <input type="radio" name="browser" value="Internet
    Explorer">Inter net Explorer<br>

    <input type="radio" name="browser" value="Netscape ">Netscape< br>

    <input type="radio" name="browser" value="Other">O ther<br>

    <hr color="red" width="200" align="left">

    <input type="radio" name="OS" value="Win 95">Windows 95<br>

    <input type="radio" name="OS" value="Win XP">Windows XP<br>

    <input type="radio" name="OS" value="Win 2000">Windows 2000<br>
    </form>



    </html>
  • Lasse Reichstein Nielsen

    #2
    Re: handling forms

    fomkatya@yahoo. com (Hamster) writes:
    [color=blue]
    > How can I pass user's browser, OS and resolution information to the
    > form element?[/color]

    That's the easy part. The hard part is to find the OS and browser
    (resolution is pretty standardized in screen.width, screen.height
    and screen.pixelDep th).

    To set a radiobutton in your form, use the following function:
    ---
    function setRadio(form,r adioGroupName,v alue) {
    var rg = form.elements[radioGroupName];
    if (!rg.length) { // not a group, so only one element
    rg.checked = true;
    return;
    }
    for (var i=0;i<rg.length ;i++) {
    if (rg[i].value == value) {
    rg[i].checked = true;
    return;
    }
    }
    }
    ---
    Then call it as, e.g.,
    ---
    setRadio(docume nt.forms["form1"],"browser","Net scape");
    ---
    [color=blue]
    > I need to make radiobuttons to be checked by default according to the
    > user's information.[/color]

    Use the above function after the page has loaded.
    [color=blue]
    > For example,
    > if visitor's browser is IE I would like the radiobutton with
    > value="Internet Explorer" to be checked by default.[/color]

    if (browseIsIE) {
    setRadio(docume nt.forms["form1"],"browser","Int ernet Explorer");
    }
    [color=blue]
    > <script language="JavaS cript">[/color]

    In HTML 4, the type attribute is required, and it is sufficient in
    all versions.
    <script type="text/javascript">
    [color=blue]
    > var browserName = navigator.appNa me
    > var browserVersion = navigator.appVe rsion
    > var OS = navigator.userA gent[/color]

    You can't trust these values if the browser tries to look like another
    browser. Since the user has a chance to correct the values, it only means
    that the initial setting is the one the user has asked his browser to
    show, so it's an acceptable mistake.

    Add:
    ---
    function init() {
    var detectedOS;
    var detectedBrowser ;
    // determine browser and OS
    setRadio(docume nt.forms['form1'],"browser",dete ctedBrowser);
    setRadio(docume nt.forms['form1'],"OS",detectedO S);
    }
    [color=blue]
    > </script>
    >
    > </head>[/color]

    You are missing the <body> tag. Always validate your code before
    asking for help!

    <body onload="init()" >
    [color=blue]
    > <input type="radio" name="browser" value="Internet
    > Explorer">Inter net Explorer<br>
    >
    > <input type="radio" name="browser" value="Netscape ">Netscape< br>[/color]

    The difference between Netscape 4 and Netscape 7 is bigger than
    the difference between Netscape 7 and IE 6.

    What if I use Mozilla, which shows pages exactly the same as Netscape
    7? Should I select Netscape?
    [color=blue]
    > <input type="radio" name="browser" value="Other">O ther<br>[/color]

    Good that this choice is included. We are not living in the era
    of "both browsers" any more.
    [color=blue]
    > <hr color="red" width="200" align="left">
    >
    > <input type="radio" name="OS" value="Win 95">Windows 95<br>
    >
    > <input type="radio" name="OS" value="Win XP">Windows XP<br>
    >
    > <input type="radio" name="OS" value="Win 2000">Windows 2000<br>[/color]

    What about "Other" here (non Windows and Win98, WinME, and WinNT5)?

    What is this for, anyway?

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Katya Kotova

      #3
      Re: handling forms

      Thank you very much. Your advices are very helpful.

      <What if I use Mozilla, which shows pages exactly the same <as Netscape
      <7? Should I select Netscape?

      I gave you just a part of that form, the real form has more options.


      <What about "Other" here (non Windows and Win98, WinME, <and WinNT5)?

      Actually, there is "other" in this case.






      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...