3

Sorry about the odd title, I'm not sure how to phrase the question. Basically, I was wondering if it is possible open a new window and fill it with HTML from the front-end rather than code an actual route, db calls, etc. (Yep ultra lazy.) Thinking along these lines...

var html = '<!DOCTYPE html><body><h1>Y HALO THAR</h1></body>'

window.open(html);

Am I just wishing upon a star here? Is this even possible?

AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
  • Does this answer your question? [Open a new tab/window and write something to it?](https://stackoverflow.com/questions/11965087/open-a-new-tab-window-and-write-something-to-it) – wesinat0r Feb 17 '20 at 22:45
  • Probably! This was when I was working on a project 7 years ago, so I don't think I need a new answer necessarily. I wish there was a better way to link duplicates with answers rather than questions. The question you linked has an accepted answer that no longer works, but if you scroll down you have a working example. https://stackoverflow.com/a/49019549/23875 – AlbertEngelB Feb 18 '20 at 12:21

3 Answers3

6

The first thing you need to do is make the popup have a variable -

var popup = window.open('blankPage.html');
var html = '<!DOCTYPE html><body><h1>Y HALO THAR</h1></body>';

popup.document.write(html);
Drew Dahlman
  • 4,952
  • 1
  • 22
  • 34
  • 1
    Both fitting answers, unfortunately the dataurl answer was a little more thorough and fits my needs a bit more. Thanks though! – AlbertEngelB Jan 23 '13 at 16:28
  • 1
    Totally understand - that's the great thing about development. There are TONS of ways to do something and each one can meet a specific need. Cheers! – Drew Dahlman Jan 23 '13 at 16:48
5

Edit this answer no longer works because of security features implemented that block certain data: URIs.

See Open a new tab/window and write something to it? for working answer

There is the option to use a dataurl...

window.open('data:text/html,<!DOCTYPE html><body><h1>Y HALO THAR</h1></body>');

Data URIs are currently supported by the following web browsers:

  • Firefox and all browsers that use the Mozilla Foundation's Gecko rendering engine
  • Safari, Chrome and other WebKit-based browsers
  • Opera
  • Konqueror
  • Internet Explorer 8+ (with certain limitations)
wesinat0r
  • 121
  • 8
Split Your Infinity
  • 4,159
  • 1
  • 21
  • 19
4

The suggestion to do the following

window.open('data:text/html,<!DOCTYPE html><body><h1>Y HALO THAR</h1></body>');

no longer works on Chrome due depreciation of the 'data:' allowance.

https://developers.google.com/web/updates/2017/03/chrome-58-deprecations#remove_content-initiated_top_frame_navigations_to_data_urls

If you used this previously, your code will now open a blank window instead.