开发者

window.focus() of JavaScript not working on IE9

This is my code

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ş";
        }
    }

I have no problem with it except the JavaScript part.

What I am trying to is after LoginButton is clicked opening dashboard.aspx and setting focus on it.And this code opens dashboard.aspx and sets focus in Google Chrome and Mozilla Firefox 4.However,when I try it on IE9 dashboard.aspx is opened but focus does not work and dashboard.aspx remains under the login page.

How can I set focus on a new window on IE9?


I have had a similar problem to this and it seemed to happen because in IE9 (and any IE) that the focus() method is run before the window is rendered.

To get round this there are two ways that I can see that will fix this:

  1. Set a timer to load the focus after a small amount of time.
  2. Defer the JavaScript from being read until the window is fully rendered.

The timer method to me is not my preferred choice as in my personal opinion it is messy and if the page takes longer to load than the timer you are stuck with the same problem. To implement the timer you could use something like:

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

I have set a delay of 5 seconds, which may be overkill.

The defer method is my preferred choice as I feel it is cleaner and simpler, but it may or may not work:

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


After what seemed like an eternity, my colleague figured out a way to make it work for us.

There are other references to the issue itself, http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/a250c431-9f09-441c-9b78-af067233ed78 http://support.microsoft.com/kb/979954

That said, we simply put the window.focus() in the body tag of the new (pop-up) page.

<body onload="FocusWindow()">

Defined FocusWindow() in a separate .js file as follows,

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜