Search code examples
jqueryfirefoxpopupwindow.open

jQuery/Firefox: open new window and change content (head and body)


I want to open a popup window with window.open and then changing the whole content of the window.

In IE and Chrome the following is working well, but not in firefox.

var Window= window.open("blanko.htm", "Win", "width=" + _iWidth + ",height=" + _iHeight + ",resizable=yes,left=" + iLeft + ",top=" + iTop + ",toolbar=1");
var Header = "<head><link rel='Stylesheet' href='CSS.css' type='text/css' /><style type='text/css'>td,th{font-size:11px;font-family:Arial;}body{font-family:Arial;font-size:12px;}</style></head>";
var Body = "<body><div>new Content</div></body>";

First Try:

 $(Window.document).ready(function () {
        $(Window.document.head).append(Header);
        $(Window.document.body).append(Body);
    });

Second Try:

$(Window.document).ready(function () {
        $(Window.document).html(Header + Body);
});

etc.

Everything I try in this way works in IE and Chrome, but in Firefox the content is loading and after one second it is replaced by the site given in the window.open parameter (blanko.htm).


Solution

  • with pure Javascript here is a very simple solution:

    var mynewWindow = window.open("", "Win", "width=" + _iWidth + ",height=" + _iHeight + ",resizable=yes,left=" + iLeft + ",top=" + iTop + ",toolbar=1");
    
            mynewWindow.document.write(
               '<html>' + 
                 '<head>' + 
                   '<title>' + myTitle + '</title>' + 
                   '<link rel="Stylesheet" href="CSS.css" type="text/css" />' +
                   //print on screen style
                   '<style media="screen"> your CSS here </style>'+
                   //Optional: print on printer style
                   '<style media="print"> your CSS here </style>'+
                 '</head>' + 
                 '<body> <div>new Content</div> </body>' + 
               '</html>'
            );
    

    Hope that helped

    (This should work with all major internet browsers)