解决在火狐中不支持onreadystatechange的方法

用window.addEventListener来对FF进行控制,如举个例子在IE下onreadystatechange可以监听事件,但在FF下不支持onreadystatechange,解决方法是做个判断,FF下就用window.addEventListener来监听事件,IE下就用onreadystatechange就可以了,代码例子如下:

    public static string GetShowAlert(string strInfo)
        {
            string strScript = String.Empty;
            strScript = "<script language=javascript> \r\n";
            strScript += "if(window.addEventListener)\r\n";
            strScript += "{\r\n";
            strScript += "alert('" + strInfo.Replace("'", "").Replace("\n", "").Replace("\r", "") + "');\r\n";
            strScript += "}\r\n";
            strScript += "else\r\n";
            strScript += "{\r\n";
            strScript += "document.onreadystatechange = ShowAlert;\r\n";
            strScript += "function ShowAlert() \r\n";
            strScript += "{\r\n";
            strScript += "if(document.readyState == \"complete\") \r\n";
            strScript += "{\r\n";
            strScript += "alert('" + strInfo.Replace("'", "").Replace("\n", "").Replace("\r", "") + "');\r\n";
            strScript += "}\r\n";
            strScript += "}\r\n";
            strScript += "}\r\n";
            strScript += "</script>";
            return strScript;
        }

posted @ 2009-07-17 10:18  scott.pei  阅读(3396)  评论(0编辑  收藏  举报