12

Hello I have a rendered jsp that does what I want, I need to show it on the second display, how would I copy the entire dom of the current window and create a completely new window? I will later want from the master to further edit that child window and write to it. Any insight greatly appreciated.

cp.
  • 1,241
  • 5
  • 15
  • 26
  • @SLaks: agreed. Anyways, I would copy the `document.body.innerHTML` into the new page. – JCOC611 Feb 13 '11 at 02:02
  • 1
    That's a _very_ expensive operation. Do you want the entire `DOM` or just the content of the `Body Node` – Raynos Feb 13 '11 at 02:04
  • @Raynos really? It used to be the case that (at least in IE) using "innerHTML" was **much** faster than discrete DOM operations. – Pointy Feb 13 '11 at 02:05
  • possible duplicate of [Copy Current Webpage Into a New Window](http://stackoverflow.com/questions/2155122/copy-current-webpage-into-a-new-window) – SpliFF Feb 13 '11 at 02:07
  • @Pointy I meant that just arbitarly copying the entire `DOM` is expensive and probably a poor design decision. And there's a diference between copying the `DOM` & `document.body.innerHTML`. For starter's you would lose events and functions. On second thought cloning the entire evented state of a page into a new window is impossible. – Raynos Feb 13 '11 at 02:08
  • @Raynos Ok, I see what you mean, and that's all completely true. – Pointy Feb 13 '11 at 02:11
  • 1
    @Pointy With a lot of hacking and black magic is it actaully possible to clone the entire state of the page into a new window? – Raynos Feb 13 '11 at 02:12
  • Hmm ... well, that's a good question; if there's a significant amount of JavaScript state, that'd be really hard to copy if you didn't know exactly how to do it. I don't know what the use case is here (maybe it doesn't really make sense) so it's hard to say. If you copied the whole thing into a fresh page, it might make sense to just let its copy of whatever JavaScript is on the main page to just run again, but then it's working on a possibly modified DOM ... a weird situation overall, I guess. – Pointy Feb 13 '11 at 02:15
  • @Pointy there is no use case. Purely out of academic interest and self satisfaction of completing a daunting task. – Raynos Feb 13 '11 at 02:24

1 Answers1

2

That would be tricky. You can certainly open a new window and communicate with it but you can't pass DOM objects. You would basically need to convert the generated DOM to a string, pass it across to the new window and then parse it as though it was a document.

Like this: Copy Current Webpage Into a New Window

Community
  • 1
  • 1
SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • Thanks spilff: That is what I feared almost a catch-22, but I have seen code snippets that copy iframes to a new window so if it works for a branch why not the whole. I know like which hand do you put the saw in and start, grin. Could I create a hidden window, with a very simple and known top level DOM structure and then send over the rest? How do I get the child window to parse and then attach the remainder of the DOM? You responded with amazing speed, grin, thank you. – cp. Feb 13 '11 at 02:14
  • You attach source code to DOM with innerHTML and that can be done on any element, not just the document root. Therefore it wouldn't be unreasonable to load a static document first and then pass only your dynamic content in the appropriate position(s). I wouldn't worry about speed until you test it, browsers these days are damn fast. – SpliFF Feb 13 '11 at 02:54
  • Thanks Spliff. Okay so I will have a master and child window. How do I read the DOM in the master window below a common point? How do I "send" that string to the child window and more obscurely how do I parse and attach that in the child window? Almost sounds obscene. – cp. Feb 13 '11 at 23:25
  • I could explain it but as I pointed out twice now your question has been answered before. Follow the link. In short though you keep a handle (variable) that points to the new window and you interact with that window by using the variable (which is basically equivalent to the "window" variable in the local frame). – SpliFF Feb 14 '11 at 00:22