window.opener in Chrome

Discussion in 'Javascript' started by me, Jul 31, 2010.

  1. me

    me Guest


    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

    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

    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 :)

    me, Jul 31, 2010
    1. Advertisements

  2. me

    me Guest

    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 ?

    me, Jul 31, 2010
    1. Advertisements

  3. me

    me Guest

    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.

    me, Jul 31, 2010
  4. me

    David Mark Guest

    What makes you think it would? Guessing rarely leads to solutions.
    Are you sure?
    No as you have not provided nearly enough information.
    That's obviously an over-generalization in the context of JS.
    They are.
    It certainly is not.
    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
    David Mark, Jul 31, 2010
  5. I just tried that, and it works for me, so we need more information.
    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?".
    No idea. It works for me, so you must be doing something different from
    what I do.

    Lasse Reichstein Nielsen, Jul 31, 2010
  6. me

    Ry Nohryb Guest

    k= 27;
    --> 27"").opener.k
    --> 27
    Ry Nohryb, Jul 31, 2010
  7. me

    me Guest

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

    me, Jul 31, 2010
  8. me

    David Mark Guest

    You are wrong. It may have the same effect in some browsers, but
    certainly not in all.
    No. You are confusing declaring variables with adding a property to a
    host object. The two concepts aren't even in the same league.
    And that could well account for the discrepancy.
    Do so and see if the behavior changes.
    Testing pop-up windows with local security restrictions is not a
    common activity for most developers.
    It's not a list.
    David Mark, Jul 31, 2010
  9. me

    me Guest

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

    <SCRIPT TYPE="text/javascript">
    function addEvents() {
    document.getElementById("id1").onclick = imgPop;
    document.getElementById("id2").onclick = imgPop;
    function imgPop() {
    window.vLink = this;
    alert("main:" +;
    var PopWin ="", "PopUpWindow");
    if (! PopWin.vImage) {
    PopWin.location = "testpop.htm";
    } else {
    PopWin.opener = self;
    <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>

    and this is "testpop.htm":

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

    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.

    me, Jul 31, 2010
  10. me

    me Guest

    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 !

    me, Jul 31, 2010
  11. me

    Luuk Guest

    Op 31-07-10 16:45, me schreef:
    no problem here,
    also not when testing on localhost

    Chrome version: 5.0.375.125
    Luuk, Jul 31, 2010
  12. me

    L.T. Guest

    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
    on a Mac running OS X:

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

    var w = window.opener;
    alert("I am the CHILD window");
    alert("w.location=" + w.location);

    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
    When I click "OK" on Safari and Firefox, I get another popup that says

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

    I'm only mentioning property "location" as an example. Actually, I
    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., Aug 20, 2010
  13. me

    Ry Nohryb Guest

    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.
    Ry Nohryb, Aug 20, 2010
  14. me

    L.T. Guest

    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
    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?

    L.T., Aug 20, 2010
  15. me

    Ry Nohryb Guest

    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.
    Ry Nohryb, Aug 21, 2010
  16. me

    Luuk Guest

    Op 20-08-10 23:48, L.T. schreef:
    "firebug lite" also does not work* when opening the FILE

    *does not work, as in, cannot be enabled
    Luuk, Aug 21, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.