By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,624 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,624 IT Pros & Developers. It's quick & easy.

Popup and set data?

P: n/a

I'm trying to open a popup window and then set some data, but I can't
seem to make it work. I'm sure I'm missing something obvious.

popwin = document.open('mypop.html', \"external\", \"width=\" + w
+\",height=\" + h +
\",resizable=no,scrollbars=no,status=yes,location= no,toolbar=no,menubar=no\");
popwin.document.data.user=user;

mypop.html has a <form name=data> with an element named "user" in it.
Essentially, I want to be able to pop up a window with some populated
data in it, but I can't figure out how I can do it.

Thanks.

-Greg G
Jul 23 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
In article <V-********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...

I'm trying to open a popup window and then set some data, but I can't
seem to make it work. I'm sure I'm missing something obvious.

popwin =
window.open.
Not document.open.
document.open('mypop.html', \"external\", \"width=\" + w
+\",height=\" + h +
\",resizable=no,scrollbars=no,status=yes,location= no,toolbar=no,menubar=no\");
The window has to load first before you can access the document. You may need
a setTimeout before you try this: popwin.document.data.user=user;

--
--
~kaeli~
A lot of money is tainted - It taint yours and it taint mine.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2

P: n/a
kaeli wrote:
In article <V-********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...
I'm trying to open a popup window and then set some data, but I can't
seem to make it work. I'm sure I'm missing something obvious.

popwin =

window.open.
Not document.open.


That was the latest iteration I had tried. It's strange, but
document.open worked the same as window.open. Or at least, it did visually.

document.open('mypop.html', \"external\", \"width=\" + w
+\",height=\" + h +
\",resizable=no,scrollbars=no,status=yes,locatio n=no,toolbar=no,menubar=no\");

The window has to load first before you can access the document. You may need
a setTimeout before you try this:
popwin.document.data.user=user;


Hmmm. This is interesting. Here's what my code looks like now:

function setValue(win,val) {
win.document.data.user = val;
}

function popUpPassword(user) {
popwin = window.open('mypop.html', "external",
"width=500,height=500,resizable=no,scrollbars=no,s tatus=yes,location=yes,toolbar=no,menubar=no");

setTimeout('setValue(popwin,user)',1000);
}

However, when the timeout goes off, I get a javascript error saying
that "user is not defined". It seems to be complaining about the start
of the next function!

It gets weirder. I copied the above code from the "Show Source"
output. However, when I click on the link in the javascript error
window, it shows this:

<script Language="JavaScript">
<!-- Begin
function popUpPassword(URL,w,h) {
window.open(url, "external", "width=" + w +",height=" + h +
",resizable=no,scrollbars=no,status=no,location=no ,toolbar=no,menubar=no");
}
// End -->
</script>

That's just totally bizzare. I'm using Firefox. IE shows me a
similar error, but it's nearly friendly enough to show me the code.

-Greg G
Jul 23 '05 #3

P: n/a
In article <Kp********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...
setTimeout('setValue(popwin,user)',1000);
}

However, when the timeout goes off, I get a javascript error saying
that "user is not defined". It seems to be complaining about the start
of the next function!
Yeah, you can't do that.
The variables are out of scope by the time the setTimeout runs. Make them
global.

It gets weirder. I copied the above code from the "Show Source"
output. However, when I click on the link in the javascript error
window, it shows this:


What link?
Could you be having a problem with caching?
I often do with Firefox. I have to control+shift+reload sometimes.

I assume both pages are yours, correct?
Put this function in mypop.html:

<script type="text/javascript">
function setUser(user)
{
document.data.user = user;
}
</script>

Now change your popup script in the other page to:
function popUpPassword(user)
{
var win = null;
int count = 0;
win = window.open('mypop.html', "external",
"width=500,height=
500,resizable=no,scrollbars=no,status=yes,location =yes,toolbar=no,menubar=no"
);
while (win == null)
{
/* give time to load, but don't let it be an endless loop. Change value
to a higher number if the content is image heavy or takes a long time
normally. Also, if popup was blocked, win will be null. */
count ++;
if (count == 5000) break;
}
if (win == null) return false; /* Just couldn't do it. Add error handling
here if you want.*/

win.setUser(user);
return true;
}

--
--
~kaeli~
Murphy's Law #2030: If at first you don't succeed, destroy
all evidence that you tried.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4

P: n/a
kaeli wrote:
In article <Kp********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...
setTimeout('setValue(popwin,user)',1000);
}

However, when the timeout goes off, I get a javascript error saying
that "user is not defined". It seems to be complaining about the start
of the next function!

Yeah, you can't do that.
The variables are out of scope by the time the setTimeout runs. Make them
global.


Ah. I see. That's....interesting. But globalness might not help.
There's a bunch of buttons on this screen. (Oh, and is there a way to
make a form do nothing when a button is pressed? All of my buttons are
type=button but they all act like submit, and I really want the
individual button control to take over. That's not really a big deal, I
just took out the form tag. It's ok on the source page...)
It gets weirder. I copied the above code from the "Show Source"
output. However, when I click on the link in the javascript error
window, it shows this:


What link?


The link in the "javascript:" window that says there's an error.
Could you be having a problem with caching?
I don't think so. In fact, I'm having a problem where the POSTDATA
doesn't get refreshed when I reload the cgi.
I often do with Firefox. I have to control+shift+reload sometimes.

I assume both pages are yours, correct?
Put this function in mypop.html:

<script type="text/javascript">
function setUser(user)
{
document.data.user = user;
}
</script>

Now change your popup script in the other page to:
function popUpPassword(user)
{
var win = null;
int count = 0;
I had to do this as var count = 0; javascript: complained that
"int" was a reserved identifier.

win = window.open('mypop.html', "external",
"width=500,height=
500,resizable=no,scrollbars=no,status=yes,location =yes,toolbar=no,menubar=no"
);
while (win == null)
{
/* give time to load, but don't let it be an endless loop. Change value
to a higher number if the content is image heavy or takes a long time
normally. Also, if popup was blocked, win will be null. */
count ++;
if (count == 5000) break;
}
if (win == null) return false; /* Just couldn't do it. Add error handling
here if you want.*/

win.setUser(user);
No go. I get a message in the javascript: window that "win.setUser"
is not a function. Nothing happens in the popup.
return true;
}


-Greg G
Jul 23 '05 #5

P: n/a
In article <XP********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...

Yeah, you can't do that.
The variables are out of scope by the time the setTimeout runs. Make them
global.
Ah. I see. That's....interesting. But globalness might not help.
There's a bunch of buttons on this screen. (Oh, and is there a way to
make a form do nothing when a button is pressed?


Sure.
If you know for *sure* all your users have javascript, don't give the form an
action or method in the form tag or any buttons of type submit.
In the onClick for each button, set the action, method, and do form.submit().
Note that this may prevent firing of any onSubmit events, so don't count on
one. Note that it also might not be very accessible to users with
disabilities.
All of my buttons are
type=button but they all act like submit
They shouldn't. How are you coding this form?
The form tag is REQUIRED to submit to a server properly, I believe. Removing
it would not be a good idea unless the buttons aren't meant to submit data to
a server.
Could you be having a problem with caching?


I don't think so. In fact, I'm having a problem where the POSTDATA
doesn't get refreshed when I reload the cgi.


That could indicate a problem with caching. Just not your browser.
Are you using a proxy?
var win = null;
int count = 0;


I had to do this as var count = 0; javascript: complained that
"int" was a reserved identifier.


Oops, my bad.
Too much java and C lately. LOL

Okay, I just tested this and it worked fine for me. Note:
document.data.user.value (the value part was missing).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> test </title>
<script type="text/javascript">
function setUser(user)
{
document.data.user.value = user;
}
</script>

</head>

<body>
<p>Test</p>
<form name="data">
<input type="text" name="user">
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<script>
function popUpPassword(user)
{
var win = null;
var count = 0;
win = window.open('test.html', "test", "width=500,height=
500,resizable=no,scrollbars=no,status=yes,location =yes,toolbar=no,menubar=no"
);
while (win == null)
{
/* give time to load, but don't let it be an endless loop. Change value
to a higher number if the content is image heavy or takes a long time
normally. Also, if popup was blocked, win will be null. */
count ++;
if (count == 5000) break;
}
if (win == null) return false; /* Just couldn't do it. Add error handling
here if you want.*/

win.setUser(user);
return true;
}
</script>
</head>

<body>
<p>Click <a href="javascript:popUpPassword('kaeli');">here</a> to open popup
and set user.</p>

</body>
</html>

--
--
~kaeli~
Frisbeetarianism (n.), The belief that, when you die, your
soul goes up on the roof and gets stuck there.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6

P: n/a

This is so damn frustrating. Here's what I've got.

----myform.html----
<html>
<head>
<title>My Form</title>
<script type='text/javascript'>
function setValue(num) {
document.data.phone.value = num;
}
</script>
</head>

<body>
<p>
<form name="data">
<input type="text" name="phone">
</form>
</body>
</html>
----myform.html----

at the top of my index.html

<html>
<head>
<title>VLAN Lookup Index</title>
<script type='text/javascript'>
<!--
function popEdit(phone) {
var win = null;
var count = 0;

win = window.open('myform.html','foo',
'resizeable=no,width=100,height=100');
while (win == null)
{
count ++;
if (count == 5000) break;
}
if (win == null) return false; /* Just couldn't do it. Add error
handling here if you want.*/

win.setValue(phone);
return true;
}
-->
</script>
</head>

Calls to popEdit in Firefox give me

Error: win.setValue is not a function
Source File: http://ggershowitz3.progpl.ctc.net/static_ip/index.cgi
Line: 18

Calls to popEdit in Internet Explorer give me
Line: 18
Char: 4
Error: Object doesn't support this property or method
Code: 0

I don't get it. It doesn't get a whole lot simpler than that, right?

-Greg G
Jul 23 '05 #7

P: n/a
In article <Pp********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...

This is so damn frustrating. Here's what I've got.


Okay, how about you supply a URL and I see if your full script works for me?
Let's make sure it isn't something about your browser configs or some other
piece of script that is making an error or maybe it's the way you're sending
the value for phone. Sometimes a missing semi-colon in one piece does really
odd things to other pieces or errors come from the oddest places.

Oh, and you're not doing something like having the two pages be on different
domains, are you? 'Cuz you can't do that.

--
--
~kaeli~
Reading while sunbathing makes you well red.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #8

P: n/a
In article <Pp********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...

This is so damn frustrating. Here's what I've got.
at the top of my index.html
This appears to be coming from index.cgi, something I just noticed.
Did you actually view the source of the generated code from index.cgi? In the
browser with view source? Because sometimes you aren't getting what you think
you're getting.

Error: win.setValue is not a function
Source File: http://ggershowitz3.progpl.ctc.net/static_ip/index.cgi


^^^
index.cgi?
Yeah, post the source your browser is actually looking at (or a valid URL,
'cuz this one isn't coming up). I bet you're missing a quote in that perl
print statementor something. ;)
--
--
~kaeli~
All I ask is the chance to prove that money cannot make me
happy.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #9

P: n/a
Greg G wrote:
This is so damn frustrating. Here's what I've got.


(snip)

Junk it. That whole approach is bizarre, particularly that while loop.
JavaScript is largely event-driven: since the 'event' you're expecting
here is the opening of the pop-up window, you should be calling this
routine from the window.onload handler of that window - it *knows* when
it's ready:

window.onload = function()
{
var form, from, to;
if ((form = document.data)
&& (to = form.user))
{
if (opener
&& !opener.closed
&& (form = opener.document.data)
&& (from = form.user))
to.value = from.value;
}
}

This assumes you're writing from document.data.user in the opener
window to document.data.user in the pop-up.

Jul 23 '05 #10

P: n/a
In article <11*********************@o13g2000cwo.googlegroups. com>, ferndoc9
@hotmail.com enlightened us with...
Greg G wrote:
This is so damn frustrating. Here's what I've got.
(snip)

Junk it. That whole approach is bizarre, particularly that while loop.


....
This assumes you're writing from document.data.user in the opener
window to document.data.user in the pop-up.


And doesn't work at all if you aren't.

The approach I posted works (well, for me anyway, YMMV, I guess) for any
window calling the code and does not rely on matching form fields or assume
anything at all about an opener, a frame sibling, or anything else. It can be
called by the opener, another window in a frameset, or its own child.

The while loop waits for load without trying to add an event. If you prefer
to add an event, feel free. ;)
I prefer the while loop since I can then handle the lack of a load at all
(404, server error, etc). Plus I don't have to putz around with browser
compatibility with events.

As always, script leaves a lot of room for creativity. Use what works for
you.

--
--
~kaeli~
If a turtle doesn't have a shell, is he homeless or naked?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #11

P: n/a
kaeli wrote:
In article <11*********************@o13g2000cwo.googlegroups. com>, ferndoc9 @hotmail.com enlightened us with...
Greg G wrote:
This is so damn frustrating. Here's what I've got.
(snip)

Junk it. That whole approach is bizarre, particularly that while

loop.
...
This assumes you're writing from document.data.user in the opener
window to document.data.user in the pop-up.
And doesn't work at all if you aren't.


Great point (?) Could be modified in 15 sec. to transfer a simple
variable.
The approach I posted works (well, for me anyway, YMMV, I guess) for any window calling the code and does not rely on matching form fields or assume anything at all about an opener, a frame sibling, or anything else. It can be called by the opener, another window in a frameset, or its own child.

The while loop waits for load without trying to add an event. If you prefer to add an event, feel free. ;)
I prefer the while loop since I can then handle the lack of a load at all (404, server error, etc). Plus I don't have to putz around with browser compatibility with events.


No idea what relevance any of that laundry list has to
anything...what's the point of using a form of polling here? Call the
routine from the window being created (or pass the data in a
querystring to it). Why script this from the opener when the execution
context of the pop-up is completely capable of fetching its own data?

----myform.html----

<html>
<head>
<title>My Form</title>
<script type="text/javascript">

window.onload = function()
{
if (opener
&& !opener.closed
&& opener.phonenum)
document.data.phone.value = opener.phonenum;
}

</script>
</head>
<body>
<form name="data">
<input type="text" name="phone">
</form>
</body>
</html>

----myform.html----

----index.html----

<html>
<head>
<title>VLAN Lookup Index</title>
<script type="text/javascript">

var win = null;
var phonenum = '';

function popEdit(phone)
{
phonenum = phone;
win = window.open(
'myform.html',
'win*',
'width=100,heigh*t=100'
);
}

</script>
</head>
<body>
<form>
<input
type="button"
value="next"
onclick="popEdit('332-666-8739')">
</form>
</body>
</html>

----index.html----

Jul 23 '05 #12

P: n/a
kaeli wrote:
In article <11*********************@o13g2000cwo.googlegroups. com>, ferndoc9 @hotmail.com enlightened us with...
Greg G wrote:
This is so damn frustrating. Here's what I've got.
(snip)

Junk it. That whole approach is bizarre, particularly that while

loop.
...
This assumes you're writing from document.data.user in the opener
window to document.data.user in the pop-up.
And doesn't work at all if you aren't.


Great point (?) Could be modified in 15 sec. to transfer a simple
variable.
The approach I posted works (well, for me anyway, YMMV, I guess) for any window calling the code and does not rely on matching form fields or assume anything at all about an opener, a frame sibling, or anything else. It can be called by the opener, another window in a frameset, or its own child.

The while loop waits for load without trying to add an event. If you prefer to add an event, feel free. ;)
I prefer the while loop since I can then handle the lack of a load at all (404, server error, etc). Plus I don't have to putz around with browser compatibility with events.


No idea what relevance any of that laundry list has to
anything...what's the point of using a form of polling here? Call the
routine from the window being created (or pass the data in a
querystring to it). Why script this from the opener when the execution
context of the pop-up is completely capable of fetching its own data?

----myform.html----

<html>
<head>
<title>My Form</title>
<script type="text/javascript">

window.onload = function()
{
if (opener
&& !opener.closed
&& opener.phonenum)
document.data.phone.value = opener.phonenum;
}

</script>
</head>
<body>
<form name="data">
<input type="text" name="phone">
</form>
</body>
</html>

----myform.html----

----index.html----

<html>
<head>
<title>VLAN Lookup Index</title>
<script type="text/javascript">

var win = null;
var phonenum = '';

function popEdit(phone)
{
phonenum = phone;
win = window.open(
'myform.html',
'win*',
'width=100,heigh*t=100'
);
}

</script>
</head>
<body>
<form>
<input
type="button"
value="next"
onclick="popEdit('332-666-8739')">
</form>
</body>
</html>

----index.html----

Jul 23 '05 #13

P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>, ferndoc9
@hotmail.com enlightened us with...
This assumes you're writing from document.data.user in the opener
window to document.data.user in the pop-up.
And doesn't work at all if you aren't.


Great point (?) Could be modified in 15 sec. to transfer a simple
variable.


So if I have 3 documents calling it that have different fields?
I'm supposed to modify it for every page that calls it?
I think not.
(note: my stuff isn't static html; it's JSP, and field names change according
to bean properties)

The simple principle of data hiding and encapsulation can be applied to web
pages, too.
I don't see the need to create dependencies where they are not needed.
YMMV, which it obviously does.

what's the point of using a form of polling here? Call the
routine from the window being created (or pass the data in a
querystring to it).
You want to create a need to know the parameters right when the window is
opened, go for it. You want to make the child know what fields are in the
parent, go for it. I do not.
Why script this from the opener when the execution
context of the pop-up is completely capable of fetching its own data?


The window may have 8 or 18 fields that can be set or retrieved at any time
by any document in the same domain that opened it or have any relation to it.
Maybe I want to set 3 fields from one document, but 3 other fields from
another. Simple setters and getters for fields allow this with no problems.
And if the other pages change their form fields, I don't have to change the
popup document.

If you don't see a need for it, don't use it.
There are always many ways of doing things. Do what works for you.

--
--
~kaeli~
Frisbeetarianism (n.), The belief that, when you die, your
soul goes up on the roof and gets stuck there.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #14

P: n/a
kaeli wrote:
In article <Pp********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...
This is so damn frustrating. Here's what I've got.
at the top of my index.html

This appears to be coming from index.cgi, something I just noticed.
Did you actually view the source of the generated code from index.cgi? In the
browser with view source? Because sometimes you aren't getting what you think
you're getting.

Error: win.setValue is not a function
Source File: http://ggershowitz3.progpl.ctc.net/static_ip/index.cgi

^^^
index.cgi?
Yeah, post the source your browser is actually looking at (or a valid URL,
'cuz this one isn't coming up). I bet you're missing a quote in that perl
print statementor something. ;)


I was completely unable to get the setValue function to be called from
the opener. It never ran, and I always got the error message saying
that win.setValue is not a function. If I were missing a quote, it
wouldn't get to the point of trying to call the function.

Here's the myform.html that's the target of this win.open:

<html>
<head>
<title>My Form</title>
<script type='text/javascript'>
function setValue(num) {
document.data.phone.value = num;
}
</script>
</head>

<body>
<p>
<form name="data">
<input type="text" name="phone">
</form>
</body>
</html>
I've got a cgi which is generating the HTML on the fly, since I'm
reading a bunch of data out of a back-end database.

I'm still interesting in seeing this work. Is there not a utility to do
a sanity check on javascript? It seems crazy that there's no way to
debug what I've written.

Also, I was able to get the data across using a simplification of RobB's
technique. Since this is a single-purpose popup and is only going to be
called from one specific page, I did this, and it worked:

window.onload = function()
{
var form = document.information;

form.phone.value = opener.phone_value;
}

where phone_value is a global variable in the opening window.

-Greg G
Jul 23 '05 #15

P: n/a
Greg G wrote:

(snip)
[...] Is there not a utility to do
a sanity check on javascript?
lol...I think you're searching for a 'sanity clause'. Bad news: there
*is* no Sanity Clause.
Also, I was able to get the data across using a simplification of RobB's technique. Since this is a single-purpose popup and is only going to be called from one specific page, I did this, and it worked:

window.onload = function()
{
var form = document.information;

form.phone.value = opener.phone_value;
}

where phone_value is a global variable in the opening window.


Don't omit the object checks, mandatory when scripting cross-window:

window.onload = function()
{
var form = document.information;
if (form
&& opener
&& !opener.closed
&& 'undefined' != typeof opener.phone_value)
form.phone.value = opener.phone_value;
}

Jul 23 '05 #16

P: n/a
In article <i9********************@ctc.net>, gg*********@CAKEctc.net
enlightened us with...

I'm still interesting in seeing this work. Is there not a utility to do
a sanity check on javascript? It seems crazy that there's no way to
debug what I've written.


And I'm really curious why it doesn't work.
Can you put complete test code (that fails for you) at a URL I can get to so
I can see it whole?

I wonder if it's something about your browser or ...?
*scratches head*

--
--
~kaeli~
Bakers trade bread recipes on a knead-to-know basis.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.