通过Ajax在另一个页面中加载一个页面

Loading a page in another page via Ajax

本文关键字:一个 Ajax 另一个 通过 加载      更新时间:2023-09-26

我在PHP页面中创建了一个Ajax聊天,一切都运行得很顺利。然后我尝试在弹出的另一个页面的div中采用这个聊天(通过z-index上的css)。

这是代码:

function showChat(username){
                    var
                        $http,
                        $self = arguments.callee;
                    if (window.XMLHttpRequest) {
                        $http = new XMLHttpRequest();
                    } else if (window.ActiveXObject) {
                        try {
                            $http = new ActiveXObject('Msxml2.XMLHTTP');
                        } catch(e) {
                            $http = new ActiveXObject('Microsoft.XMLHTTP');
                        }
                    }
                    if ($http) {
                        $http.onreadystatechange = function()
                        {
                            if (/4|^complete$/.test($http.readyState)) {
                                window.parent.document.getElementById('chatbox').style.display = "block";
                                window.parent.document.getElementById('chatbox').innerHTML = $http.responseText;
                            }
                        };
                        $http.open('GET', "dashchat/chatbox.php?chatWith="+username, true);
                        $http.send(null);
                    }
        }

div显示,聊天文本框显示,但消息不显示ajax不会运行,当我提交消息时,它会重定向到我的sendmessage.php什么好主意吗?

Try This

function showChat(username) {
    var 
                        $http,
                        $self = arguments.callee;
    if (window.XMLHttpRequest) {
        $http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            $http = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            $http = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
    if ($http) {
        $http.onreadystatechange = function () {
            if ($http.readyState == 4) {
                window.parent.document.getElementById('chatbox').style.display = "block";
                window.parent.document.getElementById('chatbox').innerHTML = $http.responseText;
            }
        };
        $http.open('GET', "dashchat/chatbox.php?chatWith=" + username, true);
        $http.send(null);
    }
}