这就是我想要做的。我想在 javascript 中创建一个指向外部网站的隐藏 iframe,然后使用 javascript 将该隐藏 iframe 的 html 源代码获取到文本框中并作为变量(如果 iframe 未加载并询问用户,可能会进行一些错误检查重新加载...)到目前为止我试过了但是如何隐藏它并从中读取源代码?提前致谢。
<script language="JavaScript" type="text/javascript">
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src", "http://www.somesite.com/");
ifrm.style.width = 640+"px";
ifrm.style.height = 480+"px";
document.body.appendChild(ifrm);
</script>
最佳答案
由于 Same Origin Policy,您不能使用 iframe 执行此操作, 但你可以使用一些服务器端,使用 php 它将是这样的:
if (isset($_GET['url'])) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_GET['url']);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
echo curl_exec($ch);
curl_close($ch);
}
然后您可以使用 ajax 调用该脚本(它将使用 jQuery)
$.get('script.php', {url: 'http://www.somesite.com/'}, function(page) {
alert(page);
});
关于javascript - 如何在 javascript 中创建一个隐藏的 iframe 并将其源代码放入文本框和作为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795050/