質問

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?

役に立ちましたか?

解決

You need to do window.open, not document.open:

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

BUT, most browsers will block popups, you may just want to do

alert("HELLO WORLD");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top