Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

window.opener in Chrome

4,767 views
Skip to first unread message

me

unread,
Jul 30, 2010, 9:05:30 PM7/30/10
to
Hello,

I have this script that works in FF and IE, but not in Chrome. I've narrowed
the problem down to this:

1. A script in the main window creates a global variable by declaring it
outside of any function
2. The variable is given a value MyVar = getElementById("MyId")
2. The script opens a popup window
3. A script in the popup window uses window.opener.MyVar to access the
variable

The window.opener doesn't seem to work as expected in Chrome; it points to
the main window (I can use window.opener.close() to close the main window)
but I cannot access the global variable. I tried
window.opener.getAttribute("MyVar") but that doesn't work either. And using
window.opener.document.title caused an error too, so apparently I cannot
access anything except methods. It's not a cross-domain security issue
either.

Anyone have any idea what's going on ? Any help/suggestions welcomed !
(though not of the "global vars are shit", "popup windows are shit", "Chrome
is shit" or "you are shit" variety :-)

Marc.


me

unread,
Jul 30, 2010, 11:11:28 PM7/30/10
to
"me" <m...@example.com> wrote in message news:QCK4o.6$Y21.1@hurricane...

> The window.opener doesn't seem to work as expected in Chrome; it points to
> the main window (I can use window.opener.close() to close the main window)
> but I cannot access the global variable. I tried
> window.opener.getAttribute("MyVar") but that doesn't work either.

Apparently there are 2 alternative syntaxes:
1. window.opener.MyVar
2. window.opener.window.MyVar
Both seem to work in IE7 and FF (I can't test other browsers right now)
Is version 2 more correct/standard-compliant/cross-browser-supported ?

Marc.


me

unread,
Jul 31, 2010, 12:35:08 AM7/31/10
to
"me" <m...@example.com> wrote in message news:VsM4o.13$VW2.2@hurricane...

> Apparently there are 2 alternative syntaxes:
> 1. window.opener.MyVar
> 2. window.opener.window.MyVar
> Both seem to work in IE7 and FF (I can't test other browsers right now)
> Is version 2 more correct/standard-compliant/cross-browser-supported ?

Apparently "window" has a property "window"... You can keep adding
.window.window.window and it should make no difference; so the above is
probably not a fix for the window.opener.MyVar problem in Chrome.

Marc.


David Mark

unread,
Jul 31, 2010, 3:52:38 AM7/31/10
to
On Jul 30, 9:05 pm, "me" <m...@example.com> wrote:
> Hello,
>
> I have this script that works in FF and IE, but not in Chrome. I've narrowed
> the problem down to this:
>
> 1. A script in the main window creates a global variable by declaring it
> outside of any function
> 2. The variable is given a value MyVar = getElementById("MyId")
> 2. The script opens a popup window
> 3. A script in the popup window uses window.opener.MyVar to access the
> variable
>
> The window.opener doesn't seem to work as expected in Chrome; it points to
> the main window (I can use window.opener.close() to close the main window)
> but I cannot access the global variable. I tried
> window.opener.getAttribute("MyVar") but that doesn't work either.

What makes you think it would? Guessing rarely leads to solutions.

> And using
> window.opener.document.title caused an error too, so apparently I cannot
> access anything except methods. It's not a cross-domain security issue
> either.

Are you sure?

>
> Anyone have any idea what's going on ?

No as you have not provided nearly enough information.

> Any help/suggestions welcomed !
> (though not of the "global vars are shit",

That's obviously an over-generalization in the context of JS.

> "popup windows are shit",

They are.

> "Chrome is shit"

It certainly is not.

> or "you are shit" variety :-)
>

Well, you are shit out of luck anyway (at least until you post more
information or a link to an example page that demonstrates the
problem).

Lasse Reichstein Nielsen

unread,
Jul 31, 2010, 5:18:13 AM7/31/10
to
"me" <m...@example.com> writes:

> I have this script that works in FF and IE, but not in Chrome. I've narrowed
> the problem down to this:
>
> 1. A script in the main window creates a global variable by declaring it
> outside of any function
> 2. The variable is given a value MyVar = getElementById("MyId")
> 2. The script opens a popup window
> 3. A script in the popup window uses window.opener.MyVar to access the
> variable

I just tried that, and it works for me, so we need more information.

> The window.opener doesn't seem to work as expected in Chrome; it points to
> the main window (I can use window.opener.close() to close the main window)
> but I cannot access the global variable. I tried
> window.opener.getAttribute("MyVar") but that doesn't work either. And using
> window.opener.document.title caused an error too, so apparently I cannot
> access anything except methods. It's not a cross-domain security issue
> either.

You are just saying that it "doesn't work". HOW does it not work?
Does it throw an exeception (and if so, which)? Does it read an incorrect
value (and if so, which)?

I.e. you should answer the questions: What did you do? What did you
expect to happen? What actually happened? (In sufficient detail so
that we can reproduce, recognize and repair the error).

A link to a page and the version of Chrome you are using is the simplest
way to answer "what did you do?".

> Anyone have any idea what's going on ? Any help/suggestions welcomed !
> (though not of the "global vars are shit", "popup windows are shit", "Chrome
> is shit" or "you are shit" variety :-)

No idea. It works for me, so you must be doing something different from
what I do.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

Ry Nohryb

unread,
Jul 31, 2010, 5:32:44 AM7/31/10
to
On Jul 31, 6:35 am, "me" <m...@example.com> wrote:
> "me" <m...@example.com> wrote in messagenews:VsM4o.13$VW2.2@hurricane...

k= 27;
--> 27

window.open("").opener.k
--> 27
--
Jorge.

me

unread,
Jul 31, 2010, 7:56:50 AM7/31/10
to

"David Mark" <dmark....@gmail.com> wrote in message
news:9177d0b1-a4a4-4238...@y12g2000prb.googlegroups.com...

On Jul 30, 9:05 pm, "me" <m...@example.com> wrote:
>> The window.opener doesn't seem to work as expected in Chrome; it points
>> to
>> the main window (I can use window.opener.close() to close the main
>> window)
>> but I cannot access the global variable. I tried
>> window.opener.getAttribute("MyVar") but that doesn't work either.
>
>What makes you think it would? Guessing rarely leads to solutions.

Well, as I understand it, but correct me if I'm wrong, declaring a global
variable MyVar = x
is the same as window.MyVar = x. But maybe I'm confusing properties and
attributes.

>>It's not a cross-domain security issue either.
>
>Are you sure?

I was testing it with local files on my computer. I haven't put this online
yet.

>> Anyone have any idea what's going on ?
>
>No as you have not provided nearly enough information.

Sorry about that; I thought it would be something simple and silly that
would be
obvious to users in this group. I'll get back to the list with more details.

Marc.


David Mark

unread,
Jul 31, 2010, 8:11:07 AM7/31/10
to
On Jul 31, 7:56 am, "me" <m...@example.com> wrote:
> "David Mark" <dmark.cins...@gmail.com> wrote in message

>
> news:9177d0b1-a4a4-4238...@y12g2000prb.googlegroups.com...
> On Jul 30, 9:05 pm, "me" <m...@example.com> wrote:
>
> >> The window.opener doesn't seem to work as expected in Chrome; it points
> >> to
> >> the main window (I can use window.opener.close() to close the main
> >> window)
> >> but I cannot access the global variable. I tried
> >> window.opener.getAttribute("MyVar") but that doesn't work either.
>
> >What makes you think it would?  Guessing rarely leads to solutions.
>
> Well, as I understand it, but correct me if I'm wrong, declaring a global
> variable MyVar = x
> is the same as window.MyVar = x.

You are wrong. It may have the same effect in some browsers, but
certainly not in all.

> But maybe I'm confusing properties and
> attributes.

No. You are confusing declaring variables with adding a property to a
host object. The two concepts aren't even in the same league.

>
> >>It's not a cross-domain security issue either.
>
> >Are you sure?
>
> I was testing it with local files on my computer.

And that could well account for the discrepancy.

> I haven't put this online
> yet.

Do so and see if the behavior changes.

>
> >> Anyone have any idea what's going on ?
>
> >No as you have not provided nearly enough information.
>
> Sorry about that; I thought it would be something simple and silly that
> would be
> obvious to users in this group.

Testing pop-up windows with local security restrictions is not a
common activity for most developers.

> I'll get back to the list with more details.
>

It's not a list.

me

unread,
Jul 31, 2010, 10:35:27 AM7/31/10
to
"Lasse Reichstein Nielsen" <lrn.u...@gmail.com> wrote in message
news:y6csko...@gmail.com...

> I just tried that, and it works for me, so we need more information.
> I.e. you should answer the questions: What did you do? What did you
> expect to happen? What actually happened? (In sufficient detail so
> that we can reproduce, recognize and repair the error).

I made a simple test that demonstrates the unexpected Chrome behaviour;
This is "testmain.htm":

<HTML><HEAD>
<TITLE>main</TITLE>
<SCRIPT TYPE="text/javascript">
function addEvents() {
document.getElementById("id1").onclick = imgPop;
document.getElementById("id2").onclick = imgPop;
}
function imgPop() {
window.vLink = this;
alert("main: vLink.id=" + window.vLink.id);
var PopWin = window.open("", "PopUpWindow");
if (! PopWin.vImage) {
PopWin.location = "testpop.htm";
} else {
PopWin.opener = self;
PopWin.ImageProps();
}
PopWin.focus();
return(false);
}
</SCRIPT>
</HEAD>
<BODY onLoad="addEvents()">
<A ID="id1" HREF="photo1.jpg"><IMG SRC="thumb1.jpg" ALT="alt 1"></A>
<A ID="id2" HREF="photo2.jpg"><IMG SRC="thumb2.jpg" ALT="alt 2"></A>
</BODY></HTML>

and this is "testpop.htm":

<HTML><HEAD>
<TITLE>pop-up</TITLE>
<SCRIPT TYPE="text/javascript">
function InitPop()
{
if (! window.opener || window.opener.closed || ! window.opener.vLink)
{
alert("closing pop-up; vLink=" + window.opener.vLink);
window.close();
}
window.vImage = new Image();
ImageProps();
document.body.appendChild(window.vImage);
}
function ImageProps()
{
window.vImage.src = window.opener.vLink.href;
window.vImage.alt = window.opener.vLink.firstChild.alt;
document.title = window.vImage.alt;
}
</SCRIPT>
</HEAD>
<BODY onLoad="InitPop()">
</BODY></HTML>

This should give a page with two thumbnails; if you click a thumbnail,
an image opens in a new window, and the window title is set to the
alt text of the thumbnail. If you click a thumbnail while the pop-up
window is already open, the window gets re-used.
The "opener=self" line is there to take possession of the pop-up
window in case it was opened by another instance of testmain.htm;
that way, several instances share the same popup window.
This test performs as described in IE7 and FF3.6. In Chrome 5.0
(latest version), the popup immediately closes again, because
window.opener.vLink is undefined. I tested with local files.

Marc.


me

unread,
Jul 31, 2010, 10:45:36 AM7/31/10
to
Hello again,

I put a version online, and there it works as expected.
It appears to be a security settings problem in Chrome after all.
Now I'll go and try to add localhost to trusted sites or something...
Thanks to everyone who responded !

Marc.
----
"It's not a list" LOL


Luuk

unread,
Jul 31, 2010, 11:18:21 AM7/31/10
to
Op 31-07-10 16:45, me schreef:

no problem here,
also not when testing on localhost

Chrome version: 5.0.375.125

--
Luuk

L.T.

unread,
Aug 20, 2010, 4:38:06 PM8/20/10
to
I think I'm having the same problem as Marc.

Here's a pair of simple HTML files that I've run on Safari, Firefox, and
Chrome
on a Mac running OS X:

parent.html
========
<script>
alert("I am the PARENT window");
alert("window.location=" + window.location);
</script>
Click "Open Window" to open a child window.
<br><br>
<a href="child.html" target="_blank">Open Window</a>

child.html
=======
<script>
var w = window.opener;
alert("I am the CHILD window");
alert("w.location=" + w.location);
</script>

In each browser, I load "parent.html". It displays two alert pop-ups,
then a
link to "Open Window".

If I click the link, I get an alert popup that says "I am the CHILD
window".
When I click "OK" on Safari and Firefox, I get another popup that says
"w.location=(path)/parent.html".

But on Chrome, I never get that second alert popup after "I am the CHILD
window".

I'm only mentioning property "location" as an example. Actually, I
haven't
found any property that I can access from the object returned by
"window.opener" in child.html.

Please tell me a way to access the properties of the object returned by
"window.opener" in Chrome.

I'm running Chrome 5.0.375.127

L.T.


*** Sent via Developersdex http://www.developersdex.com ***

Ry Nohryb

unread,
Aug 20, 2010, 5:33:12 PM8/20/10
to
On Aug 20, 10:38 pm, L.T. <anonym...@devdex.com> wrote:
> (...)

> Please tell me a way to access the properties of the object returned by
> "window.opener" in Chrome.

What you're doing works fine for me -even in Chrome- with the http://
protocol, it's when using the file:// protocol that doesn't work.
--
Jorge.

L.T.

unread,
Aug 20, 2010, 5:48:06 PM8/20/10
to
Hi, Jorge. Yeah, when I later put some code into our application based
on how I
expected it to work, thinking I'd just have to document the fact that it
wouldn't
work correctly in Chrome, I discovered that it DID work in Chrome. :0)

Do you understand why it works as HTTP everywhere, and as FILE almost
everywhere, but not as FILE in Chrome?

LT

Ry Nohryb

unread,
Aug 21, 2010, 4:38:28 AM8/21/10
to
On Aug 20, 11:48 pm, L.T. <anonym...@devdex.com> wrote:
> Hi, Jorge.  Yeah, when I later put some code into our application based
> on how I
> expected it to work, thinking I'd just have to document the fact that it
> wouldn't
> work correctly in Chrome, I discovered that it DID work in Chrome.  :0)
>
> Do you understand why it works as HTTP everywhere, and as FILE almost
> everywhere, but not as FILE in Chrome?

No, I have no idea. But that shouldn't be a -big- problem. Firstly,
because you've got all but one browsers to run/develop/debug it in.
And secondly, because if you must do it in Chrome, you can either
setup an http server in localhost and work through it, or upload it
somewhere and work remotely.
--
Jorge.

Luuk

unread,
Aug 21, 2010, 5:42:39 AM8/21/10
to
Op 20-08-10 23:48, L.T. schreef:

"firebug lite" also does not work* when opening the FILE
(http://getfirebug.com/releases/lite/chrome/)

*does not work, as in, cannot be enabled

--
Luuk

0 new messages