XMLHttpRequest responseText获取完整的源代码 [英] XMLHttpRequest responseText get the full source code
问题描述
我想获取页面的完整源代码,例如www.google.com
I want to get the the full source code of a page e.g. www.google.com
我有一个PHP文件可以复制页面的源代码:
I got a PHP file to copy the source code of a page:
<?php
echo file_get_contents('http://www.google.com');
?>
我获取源代码的功能:
function sendRequest() {
var xmlhttp;
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){
var source_code = xmlhttp.responseText;
alert(source_code);
var element = $(source_code).filter("#specificelement").html();
}
}
xmlhttp.open("GET","test2.php",true);
xmlhttp.send();
}
1)为什么不显示完整的源代码?是否有限制或我做错了什么? 我想在源代码中得到一个特定的元素.
1)Why doesn't it show the full source code? Is there a limit or am I doing something wrong? I want to get a specific element within the source code.
2)如果我能够获得特定的元素,是否可以在src路径中添加一些内容?实际图像在原始页面的外部服务器上.因此,我需要在实际映像之前添加服务器.
2) If I'm able to get the specific element, is there a way to add something to the src path? The actual images are on the external server of the original page. Therefore I need to add the server before the actual image.
现在:<img src="/images/blah.jpg" />
我想要的是:<img src="http://server.com/images/blah.jpg" />
更新:
如果执行此操作,则完整的源代码将显示在文本区域中.
If I do this the full source code is shown in the text area.
document.getElementById("textarea").innerHTML = source_code;
是否可以从此处仅获取特定元素的代码?
Is there a way to get only the code of a specific element from here?
推荐答案
当您通过file_get_contents获取代码时,AJAX请求会插入该代码,因为它是其功能,因此AJAX会插入响应.
When you are getting the code through file_get_contents, the AJAX Request interprate that code, because it's its functionality, AJAX interprate the response.
祝你好运.
这篇关于XMLHttpRequest responseText获取完整的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!