request-text.js

来自「这是《JavaScript学习源代码》一书的源代码」· JavaScript 代码 · 共 38 行

JS
38
字号
/* 

This example must be run from a web server... for example by uploading to your ISP and viewed over the web, or placing the source files in the web pages directory of a web server on your own system and viewed by entering http://localhost/http-request.html in your browser address field - assuming your server alias is "localhost" as usual.

*/

var http_request = false;
											// global variable for HTTPRequest									
// function to request text from a specified url...
function make_request()									// execute an HTTPRequest 
{								
	if (window.XMLHttpRequest)							// create an HTTPRequest object...	
	{										// for IE7, Mozilla, Safari ...
		http_request = new XMLHttpRequest();				
	}
	else if (window.ActiveXObject)							// or create an HTTPRequest object...
	{										// for Internet Explorer 6...
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else return false;								// or quit...

	http_request.open("GET","data.txt",true);					// make the request to the server...
        http_request.send(null);							// stating method, filename, async 
	http_request.onreadystatechange=show_text;  
}


function show_text()
{
  if(http_request.readyState == 4)							// if complete
  {
    if(http_request.status == 200)							// if OK
    {	
      window.document.getElementById("cell").innerHTML = http_request.responseText;	// assign the returned text
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?