How do I write content to another browser window using Javascript?

I've opened a new window with and I want to use the reference from the call to then write content to the new window. I've tried copying HTML from the old window to the new window by using myWindow.document.body.innerHTML = oldWindowDiv.innerHTML; but that's doesn't work. Any ideas?



The reference returned by is to the child window's window object. So you can do anything you would normally do, here's an example:

var myWindow ='...')
myWindow.document.getElementById('foo').style.backgroundColor = 'red'

Bear in mind that this will only work if the parent and child windows have the same domain. Otherwise cross-site scripting security restrictions will stop you.


I think this will do the trick.

   function popUp(){

    var newWindow ="","Test","width=300,height=300,scrollbars=1,resizable=1")

    //read text from textbox placed in parent window
    var text = document.form.input.value

    var html = "<html><head></head><body>Hello, <b>"+ text +"</b>."
    html += "How are you today?</body></html>"

    newWindow .document.write(html)
    newWindow .document.close()


The form solution that Vijesh mentions is the basic idea behind communicating data between windows. If you're looking for some library code, there's a great jQuery plugin for exactly this: WindowMsg (see link at bottom due to weird Stack Overflow auto-linking bug).

As I described in my answer here: How can I implement the pop out functionality of chat windows in GMail? WindowMsg uses a form in each window and then the window.document.form['foo'] hash for communication. As Dan mentions above, this does only work if the window's share a domain.

Also as mentioned in the other thread, you can use the JSON 2 lib from to serialize javascript objects for sending between windows in this manner rather than having to communicate solely using strings.




Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.