如何解决给定URL,以字符串形式返回HTML内容Javascript功能
您需要在readystate == 4时返回,例如
function httpGet(theUrl)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}
解决方法
我想编写一个JavaScript函数,该函数以给定URL的字符串形式返回HTML内容。我在Stackoverflow上找到了类似的答案。
但是,似乎document.write()
什么也没写。加载页面时,出现空白屏幕。
<html>
<head>
</head>
<body>
<script type="text/JavaScript">
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET",theUrl,false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
document.write(httpGet("https://stackoverflow.com/"));
</script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。