Add content to a new open window

I don't know how to solve this issue, I've trying reading many post but no one answer to it.

I need to open a new window with a page already coded (inside the same domain) and add some content.

The problem is that if I use OpenWindow.write() the page is not loaded yet or it overrides everything and only the code added through write appears.

var OpenWindow ='mypage.html','_blank','width=335,height=330,resizable=1');

output is the code I need to append.

I need it to work at least on Firefox, IE and GC.

Thanks in advance. It is not a problem if I need to use JQuery.



in parent.html:

<script type="text/javascript">
    $(document).ready(function () {
        var output = "data";
        var OpenWindow ="child.html", "mywin", '');
        OpenWindow.dataFromParent = output; // dataFromParent is a variable in child.html

in child.html:

<script type="text/javascript">
    var dataFromParent;    
    function init() {

When You want to open new tab/window (depends on Your browser configuration defaults):

output = 'Hello, World!';;

When output is an Object and You want get JSON, for example (also can generate any type of document, even image encoded in Base64)

output = ({a:1,b:'2'});'data:application/json;' + (window.btoa?'base64,'+btoa(JSON.stringify(output)):JSON.stringify(output)));


Google Chrome (60.0.3112.90) block this code:

Not allowed to navigate top frame to data URL: data:application/json;base64,eyJhIjoxLCJiIjoiMiJ9

When You want to append some data to existing page

output = '<h1>Hello, World!</h1>';'output.html').document.body.innerHTML += output;

output = 'Hello, World!';'about:blank').document.body.innerText += output;

If you want to open a page or window with sending data POST or GET method you can use a code like this:

    type: "get",  // or post method, your choice
    url: yourFileForInclude.php, // any url in same origin
    data: data,  // data if you need send some data to page
    success: function(msg){
                console.log(msg); // for checking
      'about:blank').document.body.innerHTML = msg;  

Here is what you can try

  • Write a function say init() inside mypage.html that do the html thing ( append or what ever)
  • instead of OpenWindow.document.write(output); call OpenWindow.init() when the dom is ready

So the parent window will have

    OpenWindow.onload = function(){

and in the child

    function init(txt){

When you call document.write after a page has loaded it will eliminate all content and replace it with the parameter you provide. Instead use DOM methods to add content, for example:

var OpenWindow ='mypage.html','_blank','width=335,height=330,resizable=1');
var text = document.createTextNode('hi');

If you want to use jQuery you get some better APIs to deal with. For example:

var OpenWindow ='mypage.html','_blank','width=335,height=330,resizable=1');

If you need the code to run after the new window's DOM is ready try:

var OpenWindow ='mypage.html','_blank','width=335,height=330,resizable=1');
$(OpenWindow.document.body).ready(function() {


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.