JavaScript的window.focus()在IE9上不工作
本文关键字:IE9 工作 window focus JavaScript | 更新日期: 2023-09-27 18:04:25
这是我的代码
protected void LoginButton_Click(object sender, EventArgs e)
{
if (DAL.DAOKullanici.Login(KullaniciTextBox.Text,SifreTextBox.Text))
{
VeriyazPROTicari.Sessionlar.Variables.loginkontrol = true;
Session["kullaniciAdi"] = KullaniciTextBox.Text;
Session["kullaniciId"] = DAL.DAOKullanici.GetEntity(DAL.DAOKullanici.KullaniciAdiIleKullaniciIdCek(KullaniciTextBox.Text)).ID;
bool main_window_open = false;
if (!main_window_open)
{
Page.RegisterClientScriptBlock("Main_Window", "<script>" +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} "
+ "</script>");
main_window_open = true;
}
HataLabel.Text = "";
}
else
{
HataLabel.Text="Hatalı Giriş";
}
}
除了JavaScript部分,我对它没有任何问题。
我想做的是在LoginButton
被点击打开仪表板之后。Aspx和设置焦点。这段代码打开仪表盘。并将重点放在Google Chrome和Mozilla Firefox 4上。然而,当我在IE9的仪表板上尝试时。Aspx打开了,但焦点和仪表板不起作用。Aspx仍然在登录页面下面。
在IE9中如何设置对焦?
我有一个类似的问题,这似乎发生了,因为在IE9(和任何IE)的焦点()方法在窗口被渲染之前运行。
为了解决这个问题,我认为有两种方法可以解决这个问题:- 设置一个计时器,在一小段时间后加载焦点。
- 延迟JavaScript被读取,直到窗口完全渲染。
定时器方法对我来说不是我的首选,因为在我个人看来,它是混乱的,如果页面需要更长的时间来加载比定时器你被困在同样的问题。要实现计时器,您可以使用如下代码:
Page.RegisterClientScriptBlock("Main_Window", "<script>" +
"setTimeout(function() { " +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} " +
"}, 5);" +
"</script>");
我已经设置了5秒的延迟,这可能是多余的。
defer方法是我的首选,因为我觉得它更干净、更简单,但它可能起作用,也可能不起作用:
Page.RegisterClientScriptBlock("Main_Window", "<script type="text/javascript" defer="defer">" +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} "
+ "</script>");
似乎过了很长一段时间后,我的同事想出了一个办法让它为我们工作。
还有其他关于问题本身的参考,http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/a250c431-9f09-441c-9b78-af067233ed78 http://support.microsoft.com/kb/979954
也就是说,我们只是将window.focus()放在新(弹出)页面的body标签中。
<body onload="FocusWindow()">
在一个单独的.js文件中定义FocusWindow(),如下所示
function FocusWindow() {
window.focus();
}