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");
Question
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?
Solution
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");