window.opener in Chrome

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

  1. me

    me Guest

    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, Jul 31, 2010
    #1
    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 ?

    Marc.
     
    me, Jul 31, 2010
    #2
    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.

    Marc.
     
    me, Jul 31, 2010
    #3
  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
    problem).
     
    David Mark, Jul 31, 2010
    #4
  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.

    /L
     
    Lasse Reichstein Nielsen, Jul 31, 2010
    #5
  6. me

    Ry Nohryb Guest

    k= 27;
    --> 27

    window.open("").opener.k
    --> 27
     
    Ry Nohryb, Jul 31, 2010
    #6
  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
    attributes.
    I was testing it with local files on my computer. I haven't put this online
    yet.
    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.
     
    me, Jul 31, 2010
    #7
  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
    #8
  9. me

    me Guest

    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, Jul 31, 2010
    #9
  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 !

    Marc.
     
    me, Jul 31, 2010
    #10
  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
    #11
  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
    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.
     
    L.T., Aug 20, 2010
    #12
  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
    #13
  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
    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
     
    L.T., Aug 20, 2010
    #14
  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
    #15
  16. me

    Luuk Guest

    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, Aug 21, 2010
    #16
    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.