[ACCEPTED]-How do I write content to another browser window using Javascript?-dom

Accepted answer
Score: 13

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

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

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

Score: 7

I think this will do the trick.

   function popUp(){

    var newWindow = window.open("","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.open()
    newWindow .document.write(html)
    newWindow .document.close()



Score: 0

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

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

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



More Related questions