2

How would I use JavaScript to launch a new window that either contains the output of a variable or custom text? Kind of like taking the results of the document.open() command and showing it in a new window. My code:

function readmessage()
{
    var doc = document.open("text/html");
    var txt = "HELLO WORLD";
    doc.write(txt);
    doc.close();
}

readmessage();

What would be the best way to achieve this in a new window? Is this possible?

4

1 回答 1

4

你需要做window.open,而不是document.open

var win = window.open();
var txt = "HELLO WORLD";
win.document.write(txt);

但是,大多数浏览器会阻止弹出窗口,您可能只想这样做

alert("HELLO WORLD");
于 2013-05-23T19:34:56.320 回答