简体   繁体   English

在div内加载外部网址,例如Google搜索

[英]load external url inside div like google search

I want to load external websites inside div and make it a bit smaller to accommodate inside div more properly. 我想在div内加载外部网站,并使其更小些,以更适当地容纳div内。
just like Google search do 就像Google搜索一样


I tried this: 我尝试了这个:

$("#targetDiv").load("www.google.com");

but it is not working. 但它不起作用。

I tried iframe but it has still 2 problems: 我尝试过iframe,但仍然有2个问题:

  1. scrolling is still enabled by pressing arrow keys & PGUP PGDOWN 仍然可以通过按箭头键和PGUP PGDOWN启用滚动
  2. how to make contents inside iframe smaller 如何缩小iframe中的内容

Don't know which method i should use 不知道我应该使用哪种方法
which is more optimized 更优化
or any alternative? 或其他选择?

What you're trying to do is not going to work. 您要尝试执行的操作将无法正常工作。 Unfortunately, JavaScript isn't allowed to make cross-domain requests for security reasons (reference: http://en.wikipedia.org/wiki/Same%5Forigin%5Fpolicy ). 不幸的是,出于安全原因,不允许JavaScript发出跨域请求(请参阅: http : //en.wikipedia.org/wiki/Same%5Forigin%5Fpolicy )。

If you create a script written in PHP that resides on your own server that submits the request, that could work but the user wouldn't have a valid session and there's a risk that the URL (links) from the other site won't work if they're relative. 如果您创建用PHP编写的脚本,该脚本位于提交请求的您自己的服务器上,则可以工作,但是用户将没有有效的会话,并且存在来自其他站点的URL(链接)无法工作的风险如果他们是相对的。

Example: 例:

$('#targetDiv').load('load.php?url=www.google.com')

You could also have a look at jquery-crossframe . 您也可以看看jquery-crossframe I've never used it but it claims to do what you're looking for. 我从未使用过它,但是它声称可以满足您的需求。

The best option is to use an iframe element. 最好的选择是使用iframe元素。

You are not going to be able to load a cross domain ajax call like that with jquery. 您将无法使用jquery加载跨域ajax调用。 from http://api.jquery.com/load/ 来自http://api.jquery.com/load/

Additional Notes: Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; 附加说明:由于浏览器的安全性限制,大多数“ Ajax”请求都受相同的原始策略约束; the request can not successfully retrieve data from a different domain, subdomain, or protocol. 该请求无法成功从其他域,子域或协议检索数据。

If iframe is not an option you can retrieve the data via an ajax call to a php page using curl. 如果不是iframe选项,则可以使用curl通过ajax调用对php页面的数据来检索数据。

Francois is right in that your ajax requests are restricted to same origin policy. Francois是正确的,因为您的ajax请求仅限于同一来源政策。 That means you cannot load contents from other websites directly. 这意味着您不能直接从其他网站加载内容。 What your are trying to achieve, however, is possible if your source supports JSONP . 但是,如果您的源支持JSONP ,则您可能要实现的目标。 If you want to specifically load google search engine results check out Google Custom Search API 如果要专门加载Google搜索引擎结果,请查看Google自定义搜索API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM