以给定URL的forms返回HTML内容。 Javascript函数

我想写一个JavaScript函数,返回HTML内容作为string给定的函数的URL。 我在Stackoverflow上find了类似的答案。

我正在尝试使用这个答案来解决我的问题。

但是,似乎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> 

你需要返回当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(); } 

我发现的唯一一个跨站点,是这个function:

 <script type="text/javascript"> var your_url = 'http://www.example.com'; </script> <script type="text/javascript" src="jquery.min.js" ></script> <script type="text/javascript"> // jquery.xdomainajax.js ------ from padolsey jQuery.ajax = (function(_ajax){ var protocol = location.protocol, hostname = location.hostname, exRegex = RegExp(protocol + '//' + hostname), YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?', query = 'select * from html where url="{URL}" and xpath="*"'; function isExternal(url) { return !exRegex.test(url) && /:\/\//.test(url); } return function(o) { var url = o.url; if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) { // Manipulate options so that JSONP-x request is made to YQL o.url = YQL; o.dataType = 'json'; o.data = { q: query.replace( '{URL}', url + (o.data ? (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data) : '') ), format: 'xml' }; // Since it's a JSONP request // complete === success if (!o.success && o.complete) { o.success = o.complete; delete o.complete; } o.success = (function(_success){ return function(data) { if (_success) { // Fake XHR callback. _success.call(this, { responseText: data.results[0] // YQL screws with <script>s // Get rid of them .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '') }, 'success'); } }; })(o.success); } return _ajax.apply(this, arguments); }; })(jQuery.ajax); $.ajax({ url: your_url, type: 'GET', success: function(res) { var text = res.responseText; // then you can manipulate your text as you wish alert(text); } }); </script> 

获得响应后,只需调用此函数将数据附加到您的身体元素

 function createDiv(responsetext) { var _body = document.getElementsByTagName('body')[0]; var _div = document.createElement('div'); _div.innerHTML = responsetext; _body.appendChild(_div); } 

@satya代码修改如下

 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) { createDiv(xmlhttp.responseText); } } xmlhttp.open("GET", theUrl, false); xmlhttp.send(); }