Click to See Complete Forum and Search --> : How do I clone a window with contents?


scottsplace
03-19-2003, 03:00 PM
I'm a novice javascript programmer, but this sounds like it should be a simple task.

function cloneWindow() {
newWin = window.open ();
var elems = document.forms[0].elements;
for (var idx = 0; idx < elems.length; idx++) {
var newElem = elems[idx].cloneNode (true);
newWin.document.appendChild (newElem);
}
newWin.focus ();
}

Alternatively tryed
newElem = newWin.document.importNode (elems[idx], true);

The new document always return empty. So what very simple thing am I missing here?

Thanks in advance.
Scott!

Jona
03-19-2003, 04:05 PM
Two things. First I went nuts trying to figure out why it kept causing errors, only to find out you forgot the last }. Second, here's a script that will work:

<html><head>
<script>
function cloneWindow() {
var newWin = window.open("","newWin");
var elems = document.body.innerHTML;
newWin.document.write(elems);
}
</script></head>
<body>
<p><b>hi</b><i>there</i></p>
<input type=button onclick="cloneWindow()" value="clone">
</body></html>

scottsplace
03-19-2003, 04:26 PM
Yes, thanks.
This seems workable.

My intent was to selectively clone elements and do some manipulation in order to create a "printable" view. I guess I can just delete the things I don't want from the new window...

Scott!

Jona
03-19-2003, 04:30 PM
Oh... I see. You can do document.getElementById("idName") as your elems variable, and then use your for() loop and whatever to clone whatever node you're after.