Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

dom

I've opened a new window with window.open() and I want to use the reference from the window.open() 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?

like image 520
Bialecki Avatar asked Oct 04 '08 05:10

Bialecki


People also ask

Can I pass a JavaScript variable to another browser window?

Can I pass a JavaScript variable to another browser window? Passing variables between the windows (if your windows are on the same domain) can be easily done via: Cookies. localStorage.

How would you use JavaScript to open a new browser window?

In JavaScript, window. The window. open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values.

Which JavaScript method is used to write HTML output?

The write() method writes directly to an open (HTML) document stream.

What does document write () function do in JavaScript?

The document. write() method writes a string of text to a document stream opened by document.


2 Answers

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

var myWindow = window.open('...')
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.

like image 55
Dan Avatar answered Nov 15 '22 22:11

Dan


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()

    } 
like image 39
Vijesh VP Avatar answered Nov 15 '22 22:11

Vijesh VP