繁体   English   中英

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

[英]window.focus() of JavaScript not working on IE9

这是我的代码

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并将焦点设置在谷歌Chrome和Mozilla Firefox 4中。但是,当我在IE9上试用它时,dashboard.aspx是打开但焦点不起作用,dashboard.aspx仍在登录页面下。

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

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

还有其他对该问题的引用, 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()">

将FocusWindow()定义在单独的.js文件中,如下所示,

function FocusWindow() {
    window.focus();
}

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

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

  1. 设置计时器以在少量时间后加载焦点。
  2. 推迟读取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秒的延迟,这可能是矫枉过正。

延迟方法是我的首选方法,因为我觉得它更干净,更简单,但它可能会或可能不会起作用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM