0

There is such a code that sends data, depending on the visitor, I redirect it to another subdomain.

var http = new XMLHttpRequest();


var url = 'https://example/handler.php';
var params = 'key=' + 'XXX' + "&value=" + 'XXX';
http.open('POST', url, true);

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function () {
  if (http.readyState == 4 && http.status == 200) {
    document.location.href = http.responseText;
  }
 }
   http.send(params);

Initially there is a page in index.php, but if I need to redirect it to another domain?

It turns out that I am loading index.php, and then another page that returned from the subquery.

How can I block further execution of scripts, html, css, script and wait for the request to be executed from the server, and then either continue further or redirect?

I tried New Promise, did not help

the goal is to redirect the subdomain to another or continue to load the index.php page

2
  • You could inject the scripts and styles tags into the dom after your request had completed Commented Jul 5, 2020 at 15:02
  • 1
    To be honest this isn't making a lot of sense. When you redirect using document.location.href a whole new page loads and the script in current page is gone. Sounds more like a server side issue Commented Jul 5, 2020 at 15:14

1 Answer 1

1

You can do this by making a synchronous ajax call. This will block the page from loading until the request is completed.

But you might want to keep in mind that this is a bad practice.

xhr.open("POST",url,false);
Sign up to request clarification or add additional context in comments.

2 Comments

It was the right decision, I wonder if it stops working if you put it in a function.
@Daheim I don't think so, are you facing any issues?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.