469,936 Members | 2,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

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="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>
Jul 20 '05 #1
2 1917
fo******@yahoo.com (Hamster) writes:
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[i].value == value) {
rg[i].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.
<script type="text/javascript">
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!

<body onload="init()">
<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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
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!
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by WindAndWaves | last post: by
7 posts views Thread by Ori | last post: by
12 posts views Thread by scsharma | last post: by
5 posts views Thread by OHM | last post: by
5 posts views Thread by Jason MacKenzie | last post: by
8 posts views Thread by jcrouse | last post: by
44 posts views Thread by Kulgan | last post: by
5 posts views Thread by Klaudiusz Bryja | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.