PDA

View Full Version : Passing values from one window to another



duke_1222
10-13-2006, 12:40 AM
Hi there,

I am fairly new to Javascrpit. I am trying to send a certain string variable from the current page into a new page (pop up).

Currently the code below simlpy opens up "newpage.asp" in another window.
That's great.

Now I need to take a value and say place it in a text box in "newpage.asp".





<head>
<script type="text/javascript" >

function open_a_window_func()
{

open_str = "newpage.aspx";
screen_size ="height=550,width=330,top=60,left=90,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no"
;
x = window.open(open_str,'window_name_eg_myNewWindow',screen_size);
x.focus();
}

</script>
</head>

<body>
<form id="formcheck" onsubmit="open_a_window_func(this);">

</form>
</body>





Can anyone give me a few ideas?

Thank you :o

codeexploiter
10-13-2006, 04:06 AM
<html>
<head>
<title>A New Window</title>
<script type="text/javascript">
// global variable for subwindow reference
var newWindow;
// generate and fill the new window
function makeNewWindow( ) {
// make sure it isn't already opened
if (!newWindow || newWindow.closed) {
newWindow = window.open("","sub","status,height=200,width=300");
// delay writing until window exists in IE/Windows
setTimeout("writeToWindow( )", 50);
} else if (newWindow.focus) {
// window is already open and focusable, so bring it to the front
newWindow.focus( );
}
}
function writeToWindow( ) {
// assemble content for new window
var k = 90;
var newContent = "<html><head><title>Secondary Window</title></head>";
newContent += "<body><h1>This is a script-created window.</h1>";
newContent += "<scr"+"ipt type='text/javascript' language='javascript'> var localVar; localVar = "+ k +"; alert('Value stored in local variable of the new window is '+localVar+'.'); document.write('localVar value: '+localVar);s</scr"+"ipt>";
newContent += "</body></html>";
// write HTML to new window document
newWindow.document.write(newContent);
newWindow.document.close( ); // close layout stream
}
</script>
</head>
<body>
<form>
<input type="button" value="Create New Window" onclick="makeNewWindow();" />
</form>
</body>
</html>


Checkout the writeToWindow( ) to know how you can pass variables to the new popup window.

Twey
10-13-2006, 02:03 PM
Or, alternatively:
x.document.forms['form_name'].elements['text_box'].value = "new_value";

duke_1222
10-14-2006, 10:07 AM
Thanks CodeExpoliter and Twey, I'll give these suggestions a bash!

duke_1222
10-14-2006, 07:07 PM
Hi codeexpoilter,

In your function writetowindow you add the HTML and Java script in page 1 which will be used in the new page.

What if I want to open an existing page say 'page2.html'? How do I add your script to an existing page without overwriting the existing page?

I want to take the variable vlaue and then use them with my controls on page 2.





function writeToWindow( ) {
// assemble content for new window
var k = 90;
var newContent = "<html><head><title>Secondary Window</title></head>";
newContent += "<body><h1>This is a script-created window.</h1>";
newContent += "<scr"+"ipt type='text/javascript' language='javascript'> var localVar; localVar = "+ k +"; alert('Value stored in local variable of the new window is '+localVar+'.'); document.write('localVar value: '+localVar);s</scr"+"ipt>";
newContent += "</body></html>";
// write HTML to new window document
newWindow.document.write(newContent);
newWindow.document.close( ); // close layout stream



Cheers for your help