vba xmlhttp 抓取网页(Excel教程Excel函数Excel表格制作Excel2010Excel实用技巧Excel视频教程)
优采云 发布时间: 2021-10-13 00:11vba xmlhttp 抓取网页(Excel教程Excel函数Excel表格制作Excel2010Excel实用技巧Excel视频教程)
示例 1
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = OK
// ...our code here...
}
else
{
alert("Problem retrieving XML data");
}
}
}
自己试试
注意:onreadystatechange 是一个事件处理程序。它的值(state_Change)是一个函数的名字,当 XMLHttpRequest 对象的状态改变时会触发这个函数。状态从 0(未初始化)变为 4(完成)。只有当状态为 4 时,我们才执行代码。
为什么使用 Async=true?
我们的示例在 open() 的第三个参数中使用了“true”。
此参数指定是否异步处理请求。
True 表示脚本将在 send() 方法之后继续执行,而无需等待服务器的响应。
onreadystatechange 事件使代码复杂化。但这是防止代码在没有服务器响应的情况下停止的最安全方法。
通过将此参数设置为“false”,可以省略额外的 onreadystatechange 代码。如果在请求失败时是否执行其余代码无关紧要,则可以使用此参数。
自己试试