window.opener in Chrome

M

me

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.
 
M

me

me said:
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.
 
M

me

me said:
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.
 
D

David Mark

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).
 
L

Lasse Reichstein Nielsen

me said:
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
 
R

Ry Nohryb

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.

k= 27;
--> 27

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

me

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.
Are you sure?

I was testing it with local files on my computer. I haven't put this online
yet.
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.
 
D

David Mark

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.
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.
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.
 
M

me

Lasse Reichstein Nielsen said:
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.
 
M

me

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.
 
L

Luuk

Op 31-07-10 16:45, me schreef:
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.

no problem here,
also not when testing on localhost

Chrome version: 5.0.375.125
 
L

L.T.

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.
 
R

Ry Nohryb

(...)
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.
 
L

L.T.

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
 
R

Ry Nohryb

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.
 
L

Luuk

Op 20-08-10 23:48, L.T. schreef:
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

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

*does not work, as in, cannot be enabled
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top