PDA

View Full Version : how to pass parameter from popup window back to the caller window?


yrtips
01-07-2003, 09:52 AM
Hi, I am currently setting up a web site and need some help!

There are 2 forms: form1 & form2

on form1, there is simply a text box and a button to open a new window - form2.
on form2, there are 3 hyperlinked lables arranged in vertical order. what I want is: when click on one of the 3 lables, form2 will close and the lable value will pass to form1 text box.

Can someone please help?
Best reg,
Jack

glenngv
01-07-2003, 10:05 AM
in the popup:

<script>
function putLabel(lbl){
if (opener && !opener.closed) opener.document.form1NameHere.textBoxNameHere.value=lbl;
window.close();
}
</script>

to use:

<a href="javascript:putLabel('Label 1')">Label 1</a>
<a href="javascript:putLabel('Label 2')">Label 2</a>
<a href="javascript:putLabel('Label 3')">Label 3</a>

yrtips
01-07-2003, 05:47 PM
Thanks glenngv!
Correct me if I am wrong:

the code is for popup right? how is about the code in caller file?
I think I have made one mistake in my previous message: there is no form in the file. basically these are two html files. one is calling the other, and upon closing the popup, value will pass to the caller file.

I have tried to write something in caller file, but doesn't seem to work. here is the code:

<input type=text name=t><a href="ok.htm" target="new">click here</a>

Best reg,
Jack

yrtips
01-07-2003, 06:01 PM
Ok, I am providing the actual coding:

open.htm (this is the caller)


<input type=text name=t><a href="#" onclick="window.open('popup.htm')">click here</a>

============================================

popup.htm

<script>
function putLabel(lbl){
opener.document.t.value=lbl;
window.close();
}
</script>

<a href="javascript:putLabel('Label 1')">Label 1</a>
<a href="javascript:putLabel('Label 2')">Label 2</a>
<a href="javascript:putLabel('Label 3')">Label 3</a>

Mr J
01-07-2003, 10:17 PM
In the opener



<form name="f1">
<input type="text" name="t1" value="">
</form>

<P><a href="#null" onclick="window.open('popup.htm','','top=100,left=100,width=300,height=300')">open</a>




In the popup


<script>
function putLabel(lbl){
if (opener && !opener.closed){
opener.document.f1.t1.value=lbl;
window.close();
}
}
</script>


<a href="javascript:putLabel('Label 1')">Label 1</a>
<a href="javascript:putLabel('Label 2')">Label 2</a>
<a href="javascript:putLabel('Label 3')">Label 3</a>

yrtips
01-08-2003, 01:14 AM
Hi Mr. J,

Doesn the code working? Doesn't work for me.

Here is how I do:

I save opener code as open.htm; save popup code as popup.htm

Is that correct?

Jack

yrtips
01-08-2003, 01:30 AM
When I tried to click on one of the links on popup.htm, instead of closing the page and pass the value to opener, it gives me a message: "unknown page" and the title of the page is: "can not find server".

Why can not find server? Do I need to run this with a web server installed? I thought it can be just run in local mode (without any web server installed).

Jack

glenngv
01-08-2003, 01:55 AM
It should run ok even if offline. can you post the codes for the 2 pages?

yrtips
01-08-2003, 02:46 AM
open.htm
=======
<form name="f1">
<input type="text" name="t1" value="">
</form>

<P><a href="#" onclick="window.open('popup.htm','','top=100,left=100,width=300,height=300'); return false;">open</a>



popup.htm
========
<script>
function putLabel(lbl){
if (opener && !opener.closed){
opener.document.f1.t1.value=lbl;
window.close();
}
}
</script>


<a href="java script:putLabel('Label 1')">Label 1</a>
<a href="java script:putLabel('Label 2')">Label 2</a>
<a href="java script:putLabel('Label 3')">Label 3</a>


Jack

glenngv
01-08-2003, 05:23 AM
is that all?
you don't have complete tags to make it a valid html document.
The html should at least look like this:

main page:

<html>
<body>
<form name="f1">
<input type="text" name="t1" value="">
</form>

<p><a href="#" onclick="window.open('popup.htm','','top=100,left=100,width=300,height=300'); return false;">open</a>

</body>
</html>


popup page:

<html>
<head>
<script>
function putLabel(lbl){
if (opener && !opener.closed){
opener.document.f1.t1.value=lbl;
window.close();
}
}
</script>
</head>
<body>
<a href="javascript:putLabel('Label 1')">Label 1</a>
<a href="javascript:putLabel('Label 2')">Label 2</a>
<a href="javascript:putLabel('Label 3')">Label 3</a>
</body>
</html>

yrtips
01-08-2003, 05:40 AM
Great, it works now!

But just to further study the case:

The difference between my code and yours is the html tag
<html>
<body>

Why is it so important?

Thanks!
Jack

glenngv
01-08-2003, 06:22 AM
You must know that this is the basic structure of an HTML page:

<html>
<head>
<title>
... place title here
</title>
</head>
<body>
... place document contents here
<form>
...place html controls here
</form>
</body>
</html>

for more information of html tags, see the link below

http://www.w3schools.com/html/

yrtips
01-08-2003, 06:29 AM
Hi glenngv,

I have the knowledge of basic html tag. But sometime, even without these tags, the file is just running fine.

Actually what I want to know is:
Why popup.htm needs information such as <html> & <body> in order to pass back the value?

Jack

glenngv
01-08-2003, 07:49 AM
if you look at this code closely...

opener.document.f1.t1.value=lbl;

you can see the hierarchy level as follows:


window (opener)
|
|__document
|
|__form (f1)
|
|__field (t1)


without html and body tags, the page is not a valid html document, thus you can't do:

window.document.form.field

because there is no (valid) document in the first place.

hope that makes sense :)

It is always a good habit to make a valid html document even though in some cases incomplete html tags displays correctly (but runs incorrectly).

Mr J
01-08-2003, 02:46 PM
Sorry yrtips

I assumed you knew basic HTML structuring

yrtips
01-08-2003, 03:03 PM
Thanks Mr. J,
I have a better understand of HTML tag now.

Special thanks give to Glenn as well. :p

Jack

Stoffel
01-14-2003, 08:17 PM
It's a beautiful script i must say, but i have one more question

My main where i want to get the information in is located in an iframe (target="hoofd")

When I just place the code in the two html-files, it won't work cause the iframe, how can i solve that problem?

Sorry for my English but I'm from Belgium, that's why

rgbjag
09-12-2005, 05:22 PM
I have a main form that is calls (from a button event) the below code to read a database and retreive 4 or 5 fields. I want to return those fields from withing the ASP script back to the form
<HTML>
<head>
<META http-equiv="Content-Style-Type" content="text/css">
<script language="JavaScript">
function putLabel(desc){
if (opener && !opener.closed){
opener.document.createpob.desc.value=desc;
window.close();
}
}
</script>
</head>
<BODY BGCOLOR=#FFFFFF TEXT=#330099 LINK=#CC0000 LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0" basefont size="1" color="green">
<FORM id="Display" name="Display" method="post" action="itemdisplay2.asp">
<%
sub Displayitem ()
If request.form("Item") <> "" then
set fs=Server.CreateObject("Scripting.FileSystemObject")
cfile= "\\128.1.1.232\plus\inv/inv/" & request.form("ITEM")
set wfile=fs.openTextFile(cfile,1,FALSE)
counter=0
do while not wfile.AtEndOfStream
counter=counter+1
singleline=wfile.readline
If counter = 3 then
itemdesc=singleline
response.write ("Item Desc = " & itemdesc & "<br>")
end If
If counter = 28 then
oldcost=singleline
response.write ("Old Cost = " & oldcost & "<br>")
end If
If counter = 30 then
lastord=singleline
response.write ("Last Order = " & lastord & "<br>")
end If
If counter = 34 then
taxcode=singleline
response.write ("Tax Code = " & taxcode & "<br>")
end If
If counter = 36 then
gledger=singleline
response.write ("G.Ledger = " & gledger & "<br>")
end If
loop
wfile.close
set fs=nothing
set wfile=nothing
end if
End Sub
%>
<Input type="text" name="Item" value="" id="item">
<Input type="submit" value="display info">
<!INPUT TYPE='BUTTON' VALUE='Use Values' onClick="javascript:putLabel(<%=itemdesc%>)">
<INPUT TYPE='BUTTON' VALUE='Close Window' onClick="window.close()">
</FORM>
<%Displayitem%>

</BODY>
</HTML>

I can't get the asp field value (I'm trying just DESC first) - into the script to return - help anyone?