为什么赢得未定义?

时间:2016-10-21 08:29:42

标签: javascript

我在javascript写了这封聊天。现在,我可以点击openChat来致电button,这样就行了。但如果我从onmessage - 事件中调用它,我会得到

  

错误:未捕获的TypeError:无法读取属性'文档'的   未定义

我是javascript的新手而且我不知道,为什么我会收到此错误?有人可以帮帮我吗? 感谢:)

这是代码:

var app = new Vue({
    el: '#app',
    data: {
        busy: false,
        roomId: 'POWebinar',
        joinedRoom: false,
        chatWirCB: false,
        chatKundenCB: false,
        microphoneWirCB: false,
        microphoneKundenCB: false,
        user: 'Benutzer',
    },
    methods: {
    openChat: function () {
        console.log("in fkt openChat");
        win = window.open('', 'myconsole',
            'scrollbars=1, width=450,height=500'
            + ',menubar=1'
            + ',toolbar=1'
            +', location=1'
            + ',resizable=1')
        win.document.writeln("<head><meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'></head><body style='background-color:#ecf0f1'></body>");
        chat = document.getElementById("chatHtml").innerHTML;
        win.document.write(chat);
        win.document.title = "PatOrg - Live Chat";
        win.document.getElementById('input-text-chat').onkeyup = function (e) {
            if (e.keyCode != 13) return;
            // removing trailing/leading whitespace
            this.value = this.value.replace(/^\s+|\s+$/g, '');

            var a = new Date();
            var b = a.getHours(); var c = a.getMinutes(); var d = a.getSeconds();
            if (b < 10) { b = '0' + b; }
            if (c < 10) { c = '0' + c; }
            if (d < 10) { d = '0' + d; }
            var time = b + ':' + c + ':' + d;

            if (!this.value.length) return
            console.log(this.value);

            connection.send('<div id="chatSend" class="chat-OutputGET bubbleGET"> <font color="white">' + username + '(' + time + '): ' + this.value + '</font></div>');
            console.log(username);
            console.log('User (' + time + '): ' + this.value);
            appendDIV('<div id="chatGet" class="chat-OutputSEND bubble"> <font color="white"> Ich (' + time + '): ' + this.value + '</font></div>');

            this.value = '';

        };
        var chatContainer = win.document.getElementById('chat-output');
        function appendDIV(event) {
            if (event == "turnMicroOff" || event.data == "turnMicroOff" || event == "turnMicroOn" || event.data == "turnMicroOn" || event == "turnChatOn" || event.data == "turnChatOn" || event == "turnChatOff" || event.data == "turnChatOff") {
                if (event == "turnMicroOff" || event.data == "turnMicroOff") {
                    console.log("audioID: " + audioID);
                    console.log("audioID: " + audioID);
                    for (i = 0; i < audioID.length; i++) {
                        connection.streamEvents[audioID[i]].stream.mute('audio');
                    }
                }
                else if (event == "turnMicroOn" || event.data == "turnMicroOn") {
                    console.log("audioID: " + audioID);
                    console.log("audioID: " + audioID);
                    for (i = 0; i < audioID.length; i++) {
                        connection.streamEvents[audioID[i]].stream.unmute('audio');
                    }
                }
                else if (event == "turnChatOn" || event.data == "turnChatOn") {
                    document.getElementById("btnChatOpen").style.visibility = "visible";
                }
                else if (event == "turnChatOff" || event.data == "turnChatOff") {
                    document.getElementById("btnChatOpen").style.visibility = "hidden";
                }

            }
            else {
                var div = document.createElement('div');
                div.innerHTML = event.data || event;
                chatContainer.appendChild(div);
                div.tabIndex = 0;
                div.focus();
                win.document.getElementById('input-text-chat').focus();
                emojify.run(win.document.getElementById('chat-output'));
            }
        }
        connection.onmessage = appendDIV;
    }
}

})

connection.onmessage = function (event) {
console.log(event.data);
if (event == "turnMicroOff" || event.data == "turnMicroOff") {
    for (i = 0; i < audioID.length; i++) {
        console.log("audioID: " + audioID[i]);
        connection.streamEvents[audioID[i]].stream.mute('audio');
    }

}
else if (event == "turnMicroOn" || event.data == "turnMicroOn") {
    app.openChat();
    for (i = 0; i < audioID.length; i++) {
        console.log("audioID: " + audioID[i]);
        connection.streamEvents[audioID[i]].stream.unmute('audio');
    }
}
else if (event == "turnChatOn" || event.data == "turnChatOn") {
    document.getElementById("btnChatOpen").style.visibility = "visible";
}
else if (event == "turnChatOff" || event.data == "turnChatOff") {
    document.getElementById("btnChatOpen").style.visibility = "hidden";
}

}

0 个答案:

没有答案
相关问题