handling forms

H

Hamster

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="JavaScript">

var browserName = navigator.appName
var browserVersion = navigator.appVersion
var OS = navigator.userAgent
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">Internet Explorer<br>

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

<input type="radio" name="browser" value="Other">Other<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>
 
L

Lasse Reichstein Nielsen

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

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.pixelDepth).

To set a radiobutton in your form, use the following function:
---
function setRadio(form,radioGroupName,value) {
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.value == value) {
rg.checked = true;
return;
}
}
}
---
Then call it as, e.g.,
---
setRadio(document.forms["form1"],"browser","Netscape");
---
I need to make radiobuttons to be checked by default according to the
user's information.

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

if (browseIsIE) {
setRadio(document.forms["form1"],"browser","Internet Explorer");
}
<script language="JavaScript">

In HTML 4, the type attribute is required, and it is sufficient in
all versions.
var browserName = navigator.appName
var browserVersion = navigator.appVersion
var OS = navigator.userAgent

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(document.forms['form1'],"browser",detectedBrowser);
setRadio(document.forms['form1'],"OS",detectedOS);
}
</script>

</head>

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

<input type="radio" name="browser" value="Internet
Explorer">Internet Explorer<br>

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

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?
<input type="radio" name="browser" value="Other">Other<br>

Good that this choice is included. We are not living in the era
of "both browsers" any more.
<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>

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

What is this for, anyway?

/L
 
K

Katya Kotova

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top