skip to Main Content

I did something similar, but not quite what I’m looking for. What I need is to load an url that is being typed down on an input. Eg. I type down “001.xml” and submit it and that is what is going to load.

<!DOCTYPE html>
<html>
<head>
<script>
  src="https://code.jquery.com/jquery-3.4.1.js"
  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  crossorigin="anonymous"></script>
<script>
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", "", true);
  xhttp.send();

}
</script>
</head>
<body>

<div id="demo">
  <h2>Let AJAX change this text</h2>

</div>
  <input type="text" id="url"></input>
  <button type="button" onclick="loadDoc()">Change Content</button>
</body>
</html>

2

Answers


  1. Try this:

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

    Hope it helps.

    Login or Signup to reply.
  2. What do you want to load? External URL or some query string to your existing page?
    Also, you may have to change <script> tag where you load jQuery. src should be within script tag.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search