0

I have a button on my page that opens a popup window.

<div class="container-content">
    <div id="VKauth">In order to leave comments, you have to authorize via VK and grant all permissions.</div><br />
    <div>
        <a href="/authvk"
            onclick="return !window.open(this.href, 'Redirecting to VK...', 'width=500, height=500')"
            target="_blank">
            <object data="/images/VKAuth.png" type="image/png" class="authvk">
                Authorize via VK
            </object>
        </a>
    </div>
</div>

A popup window comes up with the authorization form in VK (social media website). If the users cancels authorization, VK redirects them to a PHP script on my website (inside the same popup window).

That script is as follows (I removed irrelevant logic):

echo "<script>parent.response = 'user_denied';</script>";
echo "<script>window.close();</script>";

I suppose at this point, before closing the window, it must pass the variable response with the value 'user_denied' to the parent window.

On the parent page (the same that has the button), I have a javascript as follows (uses a little of jquery):

response = {
  aInternal: 10,
  aListener: function(val) {},
  set a(val) {
    this.aInternal = val;
    this.aListener(val);
  },
  get a() {
    return this.aInternal;
  },
  registerListener: function(listener) {
    this.aListener = listener;
  }
}

response.registerListener(function(val) {
  if (response.a == 'user_denied') {
      console.log('response is', response.a);
      $("#VKauth").append('<div style="color: red;">You denied authorization! Comments are blocked!</div>');
  }
});

However, after my PHP script on the popup page finishes and, presumably, reports the variable response with the value 'user_denied' to the parent window, nothing happens at all. Even no errors in the console.

I used this answer to listen for variable changes. What am I doing wrong? A lot, I suppose.

COOLak
  • 319
  • 2
  • 15

1 Answers1

0

You can pass the data the parent window like window.opener.response = 'user_denied'; for popups and window.parent.response = 'user_denied'; for iframes. Now there is a window.postMessage() you can use for more secure data sharing between windows. https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Kubwimana Adrien
  • 1,624
  • 1
  • 6
  • 11
  • I changed `echo "";` to `echo "";`. Nothing has changed at all. – COOLak Mar 08 '19 at 23:14
  • Changing the variable value won't rerun your code. You need you use a function that does something on call. Ex: `window.opener.doSomething()` – Kubwimana Adrien Mar 10 '19 at 00:06
  • So how do I make it pass the variable to the parent window? I only was able to find `parent.var = 'value';`. – COOLak Mar 10 '19 at 16:17