Log in

View Full Version : Javascript post back



lendila
04-18-2007, 06:19 AM
I have two html pages
results.html
choose.html

results.html pop up a new window (choose.html) using Javascript.
and there are radio buttons on choose.html

I want when I choose option1, its should return my option back to results.html page.

Plz any ideas

codeexploiter
04-18-2007, 07:12 AM
Try the following

1. save the below mentioned code as result.htm (you can use any name but for this file but make sure that the second file should be choose.htm. Save these file in some other locations where there is no file for name conflict).



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var result = null;
function openWindow()
{
var win = window.open("choose.htm","","height=400,width:900");
win.focus;
}

function showResult()
{
if(result == null)
{
alert('Null value; quitting....');
return;
}

alert('The value stored in result variable is : '+ result);
return;
}
</script>
</head>

<body>
<form>
<input type="button" name="open" value="Open New Window" onclick="javascript: openWindow();" />
<input type="button" name="show" value="Show Result" onclick="javascript: showResult();" />

</form>
</body>
</html>


2. Save the below mentioned code in the same location where the file is stored. Save it as choose.htm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function copyValueToParentWindow(radioButtonValue)
{
window.opener.result = radioButtonValue;
return;
}
</script>
</head>

<body>
<form name="f1">
Are you interested in computers?
<input type="radio" name="interest" value="Yes" onclick="javascript: copyValueToParentWindow(this.value);"/> Yes
<input type="radio" name="interest" value="No" onclick="javascript: copyValueToParentWindow(this.value);" /> No
</form>
</body>
</html>


You can pass values from a child window to its parent window using window.opener

Now open the first file in browser
1. click on the first button which will open a new window in which the second file will be loaded.

2. click any one of the radio button available in the new window and then go back to the parent window (first window) and click the second button (Show Result).

3. You will be able to view the value of the radio button you've clicked on the new window