XMLHTTPREQUEST
Example of performing and synchronous web request. Note synchronous request is being deprecated
by dshilkret
HTML
Note: this code may not run in JSFiddle due to some security restrictions around https and http. You should copy the code to a local environment for testing
Clicking the "Run" button will perform a HTTP request and print the HTML below.
<br />
<input type="button" id="btnRun" value="Run" />
<div id="resultBox">
The result will appear here
</div>
JavaScript
function Run() {
// The following will perform an HTTP GET request
// and the response will be the HTML of the page
// IMPORTANT: the page you're requesting should be in the same domain.
// Unless the page has cross domain enabled
var theUrl = "http://sokhasaing.com/cors/data.html";
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
// Grab the result of the request which is
// actually the HTML of the page requested
var html = xmlHttp.responseText;
// Do something with it
// In this case, I'm just throwing it into a "DIV" box
document.getElementById("resultBox").innerText = html;
// Look for the word "html"
var found = html.match("html");
// "found.index" is the position of the word that was found
alert(found.index);
}
(function(){
// When the page is loaded bind the Run function to the button's on click
document.getElementById("btnRun").onclick = Run;
})()