1

Have called another page through XMLHttpRequest. But the called page can't return JavaScript content.

In below code it's not returning addition of 5+7.

Have gone through various google stuff but nothing seems to be working.

it would be helpful if anyone could crack that....

request_xml.php

<!DOCTYPE html>
<html>
<body>

<p id="demo">Here</p>

<script>

window.onload = function() {
    loadDoc();
};

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "call_page.php", true);
  xhttp.send();
}
</script>

</body>
</html>

call_page.php

<!DOCTYPE html>
<html>
<body>
<p>why is js not working?</p>
<script> 
    document.write(5+7);
</script>

</body>
</html>

Edit:

JavaScript is working when page directly loaded

Also, JS working inside onclick in call_page.php

Observer
  • 345
  • 1
  • 4
  • 21

1 Answers1

2

The JavaScript is loaded on page load by the browser. But here you're loading the page with AJAX, which just makes a HTTP request and returns the server's response. Since it's never loaded by browser's rendering engine, the JavaScript doesn't ever load.

You can try injecting the response into a dummy element created with document.createElement but most modern browsers won't allow you to execute JavaScript like that. So your best bet is to either find another loading vector or open that other page as a popup or an iframe.

Achshar
  • 5,153
  • 8
  • 40
  • 70