开发者

Appending to External Browser Window

I have a Windows app that contains a browser control that loads pages from my website. However, due to the Windows app, I cannot debug Javascript in the usual ways (Firebug, console, alerts, etc).

I was hoping to write a jQuery plug-in to log to an external browser window such that I can simply do something like:

$.log('test');

So far, wi开发者_JS百科th the following, I am able to create the window and display the templateContent, but cannot write messages to it:

var consoleWindow;

function getConsoleWindow() {
    if (typeof (consoleWindow) === 'undefined') {
        consoleWindow = createConsoleWindow();
    }

    return consoleWindow;
}

function createConsoleWindow() {
    var newConsoleWindow = window.open('consoleLog', '', 'status,height=200,width=300');
    var templateContent = '<html><head><title>Console</title></head>' +
                          '<body><h1>Console</h1><div id="console">' +
                          '<span id="consoleText"></span></div></body></html>';
    newConsoleWindow.document.write(templateContent);
    newConsoleWindow.document.close();
    return newConsoleWindow;
}

function writeToConsole(message) {
    var console = getConsoleWindow();
    var consoleDoc = console.document.open();
    var consoleMessage = document.createElement('span');
    consoleMessage.innerHTML = message;
    consoleDoc.getElementById('consoleText').appendChild(consoleMessage);
    consoleDoc.close();
}

jQuery.log = function (message) {
    if (window.console) {
        console.log(message);
    } else {
        writeToConsole(message);
    }
};

Currently, getElementById('consoleText') is failing. Is what I'm after possible, and if so, what am I missing?


Try adding

consoleDoc.getElementById('consoleText');

right before

consoleDoc.getElementById('consoleText').appendChild(consoleMessage);

If the line you added is the one that fails, then that means consoleDoc is not right, if the next line is the only one that fails then ..ById('consoleText') is not matching up


If I don't close() the document, it appears to work as I hoped.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜