C# JavaScript的window.focus()不适用于IE9

C# JavaScript的window.focus()不适用于IE9,c#,javascript,asp.net,internet-explorer-9,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; Sessi

这是我的密码

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ş";
        }
    }
受保护的无效登录按钮\u单击(对象发送者,事件参数e)
{
if(DAL.DAOKullanici.Login(kulanicitextbox.Text,SifreTextBox.Text))
{
VeriyazPROTicari.Sessionlar.Variables.loginkcontrol=true;
会话[“kullaniciAdi”]=KullanicitTextBox.Text;
Session[“kullanicid”]=DAL.DAOKullanici.GetEntity(DAL.DAOKullanici.kullaniciadiilekullanicidcek(kullanicextbox.Text)).ID;
bool main_window_open=false;
如果(!主窗口打开)
{
第页。RegisterClientScriptBlock(“主窗口”+
“新窗口”+
“newwindow=window.open”(“+”/dashboard/dashboard.aspx“+”,“main_app_window”,“toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,滚动条=“+”1“+”,可调整大小=1,width=“+”1280“+”,height=“+”800“+”;”+
“如果(窗口焦点)”+
“{newwindow.focus();}”
+ "");
主窗口打开=真;
}
HataLabel.Text=“”;
}
其他的
{
HataLabel.Text=“HatalıGiriş”;
}
}
除了JavaScript部分,我对它没有任何问题

我尝试的是在点击“打开dashboard.aspx”并在其上设置焦点后,打开“dashboard.aspx”并在Google Chrome和Mozilla Firefox 4中设置焦点。但是,当我在IE9 dashboard.aspx上尝试时,焦点被打开,但焦点不起作用,dashboard.aspx仍在登录页面下


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

我也遇到过类似的问题,这似乎是因为在IE9(以及任何IE)中,focus()方法在渲染窗口之前运行

要解决这个问题,我可以通过两种方法来解决:

  • 设置一个计时器,在一小段时间后加载焦点
  • 延迟读取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>");
    
    Page.RegisterClientScriptBlock(“主窗口”)+
    “设置超时(函数(){”+
    “新窗口”+
    “newwindow=window.open”(“+”/dashboard/dashboard.aspx“+”,“main_app_window”,“toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,滚动条=“+”1“+”,可调整大小=1,width=“+”1280“+”,height=“+”800“+”;”+
    “如果(窗口焦点)”+
    “{newwindow.focus();}”+
    "}, 5);" +
    "");
    
    我已经设置了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>");
    
    Page.RegisterClientScriptBlock(“主窗口”)+
    “新窗口”+
    “newwindow=window.open”(“+”/dashboard/dashboard.aspx“+”,“main_app_window”,“toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,滚动条=“+”1“+”,可调整大小=1,width=“+”1280“+”,height=“+”800“+”;”+
    “如果(窗口焦点)”+
    “{newwindow.focus();}”
    + "");
    
    我遇到了类似的问题,这似乎是因为在IE9(以及任何IE)中,focus()方法是在渲染窗口之前运行的

    要解决这个问题,我可以通过两种方法来解决:

  • 设置一个计时器,在一小段时间后加载焦点
  • 延迟读取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>");
    
    Page.RegisterClientScriptBlock(“主窗口”)+
    “设置超时(函数(){”+
    “新窗口”+
    “newwindow=window.open”(“+”/dashboard/dashboard.aspx“+”,“main_app_window”,“toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,滚动条=“+”1“+”,可调整大小=1,width=“+”1280“+”,height=“+”800“+”;”+
    “如果(窗口焦点)”+
    “{newwindow.focus();}”+
    "}, 5);" +
    "");
    
    我已经设置了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>");
    
    Page.RegisterClientScriptBlock(“主窗口”)+
    “新窗口”+
    “newwindow=window.open”(“+”/dashboard/dashboard.aspx“+”,“main_app_window”,“toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,滚动条=“+”1“+”,可调整大小=1,width=“+”1280“+”,height=“+”800“+”;”+
    “如果(窗口焦点)”+
    “{newwindow.focus();}”
    + "");
    
    在经历了似乎是永恒的过程后,我的同事找到了一种方法让它为我们工作

    还有其他关于这个问题本身的提法

    也就是说,我们只需将window.focus()放在新(弹出)页面的body标记中


    在经历了似乎是永恒的过程后,我的同事想出了一个办法让它为我们工作

    还有其他关于这个问题本身的提法

    也就是说,我们只需将window.focus()放在新(弹出)页面的body标记中


    请不要编辑标语或“谢谢(提前)”类型的信息。因为它们在这里不被接受,所以--“谢谢”和类似的结尾被视为签名的一部分。这个链接没有提到“谢谢”等。卡尔森先生,有什么不接受“谢谢”的呢?这篇元帖子--询问关于感谢的问题,杰夫·阿特伍德(Jeff Atwood)提到,这些问题更难被发现,在meta的其他地方,也有一些讨论指出,如果它不是问题描述的一部分,那么当涉及到开头和结尾时,它就不应该存在