0

I am currently using document.write to inject html into my framework. I'm using Backbone, Marionette, require.js, jQuery.

I want to avoid using document.write since it has performance and browser impacts.

Please suggest an alternative.

My current code:

    var html = this.compilePageHtml(); //We get some HTML code
    this._previewWindow = window.open("about:blank", "PreviewWindow","width=320, height=570, scrollbars=yes" ); 
    this._previewWindow.document.write(html); //Loads HTML in new window popup
Louis
  • 146,715
  • 28
  • 274
  • 320
VRD
  • 53
  • 1
  • 8

1 Answers1

2

You can just use the .innerHTML property on the body of the new window.

this._previewWindow = window.open("about:blank", "PreviewWindow","width=320, height=570, scrollbars=yes" ); 
this._previewWindow.document.body.innerHTML = html; 

Demo: http://jsfiddle.net/jfriend00/g9e8rg7h/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • This replace html in the body element, while it should be document. – MaxZoom Feb 06 '15 at 05:54
  • 1
    @MaxZoom - it depends upon what the OP's html is (which they don't disclose). A new window/document is given a skeleton body so this code will set the body HTML. Yes, you could set the `document.documentElement` HTML too. – jfriend00 Feb 06 '15 at 05:59
  • I added `this._previewWindow.document.close();` it loads instantaneously (instead of ~4-5 seconds lag) but I get error loading page by jquery.mobile . This is the div `

    Error Loading Page

    ` everything seems to be working fine but I don't get why I'm having that error.
    – VRD Feb 06 '15 at 09:01