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?

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
pattyd
  • 5,927
  • 11
  • 38
  • 57

1 Answers1

4

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");
Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
dave
  • 62,300
  • 5
  • 72
  • 93