Recent

Author Topic: XMLHttpRequest response to open new page?  (Read 8723 times)

epergola

  • Full Member
  • ***
  • Posts: 157
XMLHttpRequest response to open new page?
« on: May 10, 2020, 09:36:44 pm »
I can't get past this.
I have a synapse http server. The client side sends a request, the app (server) creates an html which is saved to the outputdata stream that is sent back to the client.
This updates the <body> on the client side.
As long as I am on the same client html page everything works fine.
But for one client request I need to open a  a new page. And here I get stuck.
Previously I add this script to the client <body>
Code: Pascal  [Select][+][-]
  1.   var xhr = new XMLHttpRequest;
  2.   xhr.onreadystatechange=function(){
  3.     if (xhr.readyState === 4 && xhr.status ===200) {
  4.    (normally, document.body.insertAdjacentHTML("beforeend", xhr.responseText);');
  5.         what should I do here for this different purpose?
  6.     }
  7.   }
  8.   xhr.open("POST","http://192.168.1.64",true);
  9.   xhr.setRequestHeader("Content-Type", "text/xml");
  10.     xhr.send();
  11.  
Normally I have this in the request processing on the server side
Code: Pascal  [Select][+][-]
  1.    l:=loadheadings(t);
  2.    l.SaveToStream(OutputData);
  3.     ..... set all appropriate headers and send them
  4.   Sock.SendBuffer(OutputData.Memory, OutputData.Size);
  5.  
The page gets updated according to the L (which is html markup).
But i I want to load a new page loaded with this L, and open it, what should I do? Perhaps save L to a disk file in the server side?
What should go in the xhr.onreadystatechange=function() and
what should I return after loadheadings() on the server side?

fjabouley

  • Full Member
  • ***
  • Posts: 129
Re: XMLHttpRequest response to open new page?
« Reply #1 on: May 10, 2020, 10:41:37 pm »
.
« Last Edit: May 10, 2020, 10:54:16 pm by fjabouley »

fjabouley

  • Full Member
  • ***
  • Posts: 129
Re: XMLHttpRequest response to open new page?
« Reply #2 on: May 10, 2020, 10:56:17 pm »
Argh editing posts is horrible....
Can you send xml from server to change innerhtml code in your client page?

epergola

  • Full Member
  • ***
  • Posts: 157
Re: XMLHttpRequest response to open new page?
« Reply #3 on: May 11, 2020, 01:05:37 am »
Thanks. Yes, but in this particular case I want to leave the current page as is, and open a new page with the html created in the response

kupferstecher

  • Hero Member
  • *****
  • Posts: 604
Re: XMLHttpRequest response to open new page?
« Reply #4 on: May 11, 2020, 01:21:11 am »
You could call a new address in the code.
https://stackoverflow.com/questions/2018567/loading-another-html-page-from-javascript

(I'm not proficient in that field, there are probably better solutions.)

fjabouley

  • Full Member
  • ***
  • Posts: 129
Re: XMLHttpRequest response to open new page?
« Reply #5 on: May 11, 2020, 07:09:27 am »
Maybe this?


Code: Text  [Select][+][-]
  1.  
  2. var xhr = new XMLHttpRequest;
  3.     xhr.onreadystatechange=function(){
  4.     if (xhr.readyState === 4 && xhr.status ===200) {
  5. var new_window = window.open(null, '','_blank');
  6. new_window.document.write(xhr.responseText);
  7.     }
  8.   }
  9.   xhr.open("POST","http://192.168.1.64",true);
  10.   xhr.setRequestHeader("Content-Type", "text/xml");
  11.   xhr.send();
  12.  
  13.  
  14.  

Remove "_blank" to open in other tab, instead of opening in a new window.
Regards
« Last Edit: May 11, 2020, 04:16:30 pm by fjabouley »

epergola

  • Full Member
  • ***
  • Posts: 157
Re: XMLHttpRequest response to open new page?
« Reply #6 on: May 12, 2020, 07:54:05 pm »
Thanks fjabouley.
That works, but the script inside the new windows (like onclick() function) do no execute.

 

TinyPortal © 2005-2018