Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
How to put the response of a post-request into a new window/tab

I have this working example of how to use POST and put the server-generated html in a new window:

const response = await this.axiosInstance.post(url, postData)
const htmlResponse = response.data
const newWindow = window.open()
newWindow.document.write(htmlResponse)

However I'm not sure if this is the best or even correct way to do it. I see a few warnings with this. I get a warning on the last line that newWindow is possibly 'null'. If switching to TypeScript I am also not sure the type of htmlResponse. I would assume Document, but write() only supports string, so it might be the correct classification.

Is there a better way to put the content of a post-request into a new window?

Update: The content-type is of type text/html; charset=UTF-8, not sure if it changes anything.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

What you have there might be enough for your use case. One might recommend waiting for some form of a ready event before manipulating the DOM.

const newWindow = window.open();
newWindow.onload = (event, f, g, h) => newWindow.document.write('Do great things.');

A much better solution might be fetching actual data instead of an HTML string from the server, creating a template page to receive the data, and rendering a well-formatted page.

The Window.postMessage() API could be used to send data between parent and child windows. This is especially useful if you need to circumvent the Same-origin policy.

I have made several assumptions, and this suggestion might not work for your use case.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!