通过XMLHttpRequest进行同步通信。
open中的第三个参数为false时就是进行同步通信。
var xhr = new XMLHttpRequest();
xhr.open('get','http://example.com/something',false);
xhr.setRequestHeader('If-Modified-Since','Thu,01 Jan 1970 00:00:00 GMT');
xhr.send(null);
if(xhr.status == 200){
alert(xhr.responseText);
}
在实际操作中不推荐使用同步通信。在客户端JavaScript最重要的是如何减少用户的等待时间,尽可能向用户提供流畅的用户体验。所以,应当避免使用同步通信来减少等待时间。