Javascript 嵌入没有 iframe 的外部页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8433319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:59:42  来源:igfitidea点击:

Embed an External Page Without an Iframe?

javascriptasp.nethtmliframeembed

提问by JacobTheDev

Is there any way to embed an external web page without using an iframe? I have access to both sites, I just want the page that the content is embedded on to resize based on the content that is embedded (it will change over time, and be on multiple sites).

有没有办法在不使用 iframe 的情况下嵌入外部网页?我可以访问这两个站点,我只希望嵌入内容的页面根据嵌入的内容调整大小(它会随着时间的推移而改变,并在多个站点上)。

Thanks!

谢谢!

EDIT: I don't think any kind of AJAX would work because it's cross-site, and JavaScript doesn't let you load off-site content (as far as I'm aware).

编辑:我认为任何类型的 AJAX 都不起作用,因为它是跨站点的,而且 JavaScript 不允许您加载站外内容(据我所知)。

采纳答案by Sudhir Bastakoti

You could load the external page with jquery:

您可以使用 jquery 加载外部页面:

<script>$("#testLoad").load("http://www.somesite.com/somepage.html");</script>
<div id="testLoad"></div>
//would this help

回答by ptriek

Or you could use the object tag:

或者你可以使用对象标签:

http://jsfiddle.net/7MaXx/

http://jsfiddle.net/7MaXx/

<!--[if IE]>
<object classid="clsid:25336920-03F9-11CF-8FD0-00AA00686F13" data="http://www.google.be">
<p>backup content</p>
</object>
<![endif]-->

<!--[if !IE]> <-->
<object type="text/html" data="http://www.flickr.com" style="width:100%; height:100%">
<p>backup content</p>
</object>
<!--> <![endif]-->

回答by talbottsw

What about something like this?

这样的事情怎么办?

<?php
$URL = "http://example.com";
$base = '<base href="'.$URL.'">';
$host = preg_replace('/^[^\/]+\/\//', '', $URL);
$tarray = explode('/', $host);
$host = array_shift($tarray);
$URI = '/' . implode('/', $tarray);
$content = '';
$fp = @fsockopen($host, 80, $errno, $errstr, 30);
if(!$fp) { echo "Unable to open socked: $errstr ($errno)\n"; exit; } 
fwrite($fp,"GET $URI HTTP/1.0\r\n");
fwrite($fp,"Host: $host\r\n");
if( isset($_SERVER["HTTP_USER_AGENT"]) ) { fwrite($fp,'User-Agent: '.$_SERVER

["HTTP_USER_AGENT"]."\r\n"); }
fwrite($fp,"Connection: Close\r\n");
fwrite($fp,"\r\n");
while (!feof($fp)) { $content .= fgets($fp, 128); }
fclose($fp);
if( strpos($content,"\r\n") > 0 ) { $eolchar = "\r\n"; }
else { $eolchar = "\n"; }
$eolpos = strpos($content,"$eolchar$eolchar");
$content = substr($content,($eolpos + strlen("$eolchar$eolchar")));
if( preg_match('/<head\s*>/i',$content) ) { echo( preg_replace('/<head\s*>/i','<head>'.

$base,$content,1) ); }
else { echo( preg_replace('/<([a-z])([^>]+)>/i',"<\1\2>".$base,$content,1) ); }
?>

回答by tildy

Question is good, but the answer is : it depends on that.

问题很好,但答案是:这取决于那个。

If the other webpage doesn't contain any form or text, for example you can use the CURL method to pickup the exact content and after then showing on your page. YOu can do it without using an iframe.

如果其他网页不包含任何表单或文本,例如您可以使用 CURL 方法提取确切的内容,然后在您的页面上显示。你可以在不使用 iframe 的情况下做到这一点。

But, if the page what you want to embed contains for example a form it will not work correctly , because the form handling is on that site.

但是,如果您要嵌入的页面包含例如表单,它将无法正常工作,因为表单处理在该站点上。

回答by martar

Why not use PHP! It's all server side:

为什么不使用PHP!这都是服务器端:

<?php print file_get_contents("http://foo.com")?>

If you own both sites, you may need to ok this transaction with full declaration of headers at the server end. Works beautifully.

如果您拥有这两个站点,则可能需要在服务器端使用完整的标头声明来确定此事务。工作精美。

回答by lafeber

HTML Imports, part of the Web Components cast, is also a way to include HTML documents in other HTML documents. See http://www.html5rocks.com/en/tutorials/webcomponents/imports/

HTML 导入是 Web 组件转换的一部分,也是将 HTML 文档包含在其他 HTML 文档中的一种方式。请参阅http://www.html5rocks.com/en/tutorials/webcomponents/imports/