[英]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();
我正在尝试获取一个弹出窗口以聚焦我尝试过
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: 我在Jquery中尝试了一下,但是还是没有用:
<script type="text/javascript">
$(document).ready(function () {
alert("test");
window.focus();
});
</script>
Flow: 流:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.