I have a button that will open a new blank window using window.open
, but will write a multiplication table on it.
Here's my code.
document.getElementById("p").innerHTML = "<button id = 'mult' type = 'button'>Multiplication Tables</button>";
document.getElementById("mult").onclick = function() {
newWindow()
};
document.getElementById("close").onclick = function() {
close()
};
function newWindow() {
var multiplyWindow = window.open("", "multiplicationTables", "width = 300, height = 200");
multiplyWindow.document.write(multiply());
}
function multiply() {
document.write("<center><table border='1px'>");
for (var a = 5; a < 11; a++) {
document.write("<tr style='height:40px'>");
for (var b = 5; b < 11; b++) {
document.write("<td style='width:40px'><center><font size='4'>" + a * b + "</center></font></td>");
}
document.write("</tr>");
}
}
<p id="p"></p>
I know the problem is because when I run the multiply()
function, we are nesting a document.write
. I thought of using document.getElementById
, but the problem with that is (I think) there is no ID of a new window opened using window.open
.
Instead, what happens is the multiplication table is printed on the original window, and in the new pop-up window, an "undefined" message shows up. This makes sense, again because of the nested document.write
s.
Two questions:
- How do I fix this nested
document.write
and still get it to work? - Is there a way to assign an ID to a new window using
window.open
?
Additionally, I look at this SO post: document.write nested in document.write, but the only answer was simply not to use document.write
.
Thank you in advance.