将网站加载到DIV中 [英] Load website into DIV

查看:91
本文介绍了将网站加载到DIV中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在文本框中输入URL并单击提交"按钮时,我实际上如何从网站检索数据.我希望将数据放入我拥有的div中.这可能吗?

how do i actually retrieve data from a website when i write the URL in a textbox and then hit the submit button. I want the data to be put in an div that i have. Is this possible?

我已经尝试过了,但是没有用!

I have tried with this, but it doesn't work!

<script type="text/javascript"> 
function contentDisp()
{
var url = $("#text").val();
$("#contentArea").load(url);
}
</script> 



<textarea id="text">Write URL here</textarea>
<br />
<input type="button" value="Click" onClick="contentDisp();">

<div id="contentArea"></div>

推荐答案

由于javascript调用中的沙箱,您无法执行此操作(不能在加载JS的域之外调用),至少不能这样做直接

Due to the sandbox on javascript calls, you can't do this (you can't call outside the domain the JS was loaded from), at least not directly

有两种解决方法.

第一个方法是使用JS设置src参数,将请求的网址加载到iframe中,而不是div中.简单,容易,但会限制您访问iframe中的数据.

The first would be to load the requested url in an iframe rather than a div by setting the src parameter using JS. Simple, easy but would limit your access to the data in the iframe.

第二个方法是在您的服务器上发出AJAX请求,然后您的服务器查找URL并返回HTML内容(使用CURL或类似方法非常容易).这样,您就可以更多地处理返回的内容,但是由于Cookie上的沙箱,您将无法再请求诸如用户facebook页面之类的内容,也无法请求任何需要会话的内容,因为它将在服务器会话中正常工作而不是浏览器会话.

The second would be to make an AJAX request on your server and your server then looks up the URL and returns the HTML content (pretty easy to do with CURL or similar). This lets you play around with the returned content a bit more, but again due to the sandbox on cookies you won't be able to request anything like a users facebook page or anything that requires a session as it would be working through the servers session and not the browser session.

这篇关于将网站加载到DIV中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆