0

I have a page called PayOut.cshtml. On this page, I have a button called Pay, which opens a new small window called Authenticate.cshtml for a user to authenticate himself by specifying his email and password.

Once a user has been authenticated, the the Authenticate.cshtml should be dismissed, and showing a button called Confirm in the PayOut.cshtml page.

I have tried the following:

public AuthenticateController(Authenticate obj)
{
      var success = false;

      if (auth) {
         success = true;
      }

      return View("close");
}

View for close:

<body>
    <script type="text/javascript">
        window.close();
    </script>
</body>

How can I dismiss the the authenticate view and show a button in the PayOut view by using session ? Please help.

2 Answers 2

2

You can use "postMessage", in the main window use something like this:

<!DOCTYPE html>
<html>
   <header>
      <title>PostMessage Demo</title>
  </header>
  <body>
    <button id="btn" onclick="openPopup();">Open Popup</button>
    <script>

        window.addEventListener("message", onMessage, false);

        function onMessage(event){
            document.getElementById("btn").innerText = "you typed " + event.data;
            document.getElementById("btn").disabled = false;
        };

        function openPopup(){
            document.getElementById("btn").textContent = "popup active";
            document.getElementById("btn").disabled = true;
            window.open("/popup", "popup window");
        }

     </script>
   </body>
</html>

Then in the popup window this:

<!DOCTYPE html>
<html>
    <header>
        <title>Popup</title>
    </header>
    <body>
        <input id="textEdit" type="text" value=""></input>
        <button onclick="_close();">Close popup</button>
        <script>

            function _close(){
            let pUri = window.location.protocol + "//" + window.location.host + "/";
            window.opener.postMessage(document.getElementById("textEdit").value, pUri);
            window.close();
        }

        </script>
    </body>
</html>

When you click the "Close popup" button in the popup window it will close and trigger the onMessage event in the main window with the text you typed in the "textEdit" input.

Sign up to request clarification or add additional context in comments.

Comments

0

For security reasons, the specs actually don't allow this. Although, I just tested this with Edge, Chrome, Firefox, and IE and it worked. Could you clarify how it didn't work?

Anyway, I decided to try another method that doesn't involve a window trying to close itself and it worked in the same four browsers.

In Payout.cshtml

   var newWindow;
    function authenticate() {
        newWindow = window.open("@Url.Action("Authenticate")");
        window.setTimeout(tryCloseWindow, 5000);
    }
    function tryCloseWindow() {
        try {
            if (newWindow.closeMe == undefined) {
                window.setTimeout(tryCloseWindow, 1000);
                return;
            }
        } catch(ex) {
            // window was closed by user
            return;
        }

        newWindow.close();
    }

Authenticate.cshtml

<button onclick="pay();">pay</button>

@section Scripts
{
    <script>
        function pay() {
            window.location = "@Url.Action("Close")";
        }
    </script>
}

Close.cshtml

@section Scripts
{
    <script>
        window.closeMe = true;
    </script>
}

2 Comments

How can I show the button Confirm when the popup window is dismissed ?
Well, after newWindow.close(), and assuming you have a button that's hidden, document.getElementById("confirmButton").style.display = "block";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.