javascriptc#jqueryasp.netasp.net-webpages

Set Focus to a popup window from C# (or JS)


I'm trying to get a popup window to focus i tried the window.open().focus(); but it keeps changing in the background so I'm trying in my code to close a window and re-opening it again.

string url = "Report.aspx";
string s = "window.open('" + url + "', 'popup_window456789123', 'width=650,height=500,left=100,top=100,resizable=yes,scrollbars=yes')";

string t = "window.close()";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", t, true);

ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);

Can anyone tell me why this doesn't work?

I tried it in Jquery but that didn't work either:

<script type="text/javascript">
    $(document).ready(function () {
        alert("test");
        window.focus();
    });
</script>

Flow:

  1. Main page: click button1 ► opens popup1
  2. popup is in focus and shows data
  3. mainpage(gets focus): click button2 ► opens same popup1
  4. popup changes data (but dos not get focus) This is the issue

Solution

  • Ok I've done a fiddle which shows what you should have. View Fiddle

    If I click off one window and click on another then it comes back (reloads) and gets focus.

    If you are expecting something like a text box getting focus or something in the loaded window then you need to have something within the page that is getting loaded to give that text box focus.

          <script> function openwin(url) {
          window.open(url, 'popup_window456789123','width=650,height=500,left=100,top=100,resizable=yes,scrollbars=yes ')
        }
    </script>
    
        <button onclick="openwin('https://www.webpagetest.org/')">
    Button1
    </button>
    <button onclick="openwin('https://www.webpagetest.org/')">
    Button2
    </button>