window.focus()的JavaScript无法在IE9上运行

时间:2011-08-04 14:36:17

标签: c# javascript asp.net internet-explorer-9

这是我的代码

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后点击打开dashboard.aspx并设置焦点,我想尝试的是这个代码。此代码打开dashboard.aspx并将焦点设置在Google Chrome和Mozilla Firefox 4中。但是,当我尝试时它在IE9上显示dashboard.aspx已打开但焦点不起作用且dashboard.aspx仍在登录页面下。

如何在IE9的新窗口上设置焦点?

2 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,它似乎发生了,因为在IE9(和任何IE)中,focus()方法在渲染窗口之前运行。

为了解决这个问题,我可以通过两种方式来解决这个问题:

  1. 设置计时器以在少量时间后加载焦点。
  2. 推迟读取JavaScript,直到窗口完全呈现。
  3. 对我来说计时器方法不是我的首选,因为在我个人看来它很乱,如果页面加载的时间比计时器长,你遇到同样的问题。要实现计时器,您可以使用以下内容:

    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秒的延迟,这可能有点过分。

    延迟方法是我的首选方法,因为我觉得它更干净,更简单,但它可能有效,也可能无效:

    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>");
    

答案 1 :(得分:2)

看似永恒之后,我的同事找到了一种让它适合我们的方法。

此问题本身还有其他参考,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();
}