【发布时间】:2012-05-25 10:01:22
【问题描述】:
我想编写一个 javascript 函数,将 HTML 内容作为给定 URL 的字符串返回给函数。我在 Stackoverflow 上找到了类似的答案。
我正在尝试使用this answer 来解决我的问题。
然而,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>
【问题讨论】:
-
使用 .innerHTML,而不是 document.write。
-
你的意思是像
httpGet("http://stackoverflow.com/").innerHTML;吗?这也加载了一个空白页面。 -
下面的答案描述了您的需求。
-
Chrome 77.0.3865.90,收到警告
(index):40 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
标签: javascript html