Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 为什么我在我的网站iframe中嵌入的外部网站会自动重定向到外部网站页面_Html_Iframe - Fatal编程技术网

Html 为什么我在我的网站iframe中嵌入的外部网站会自动重定向到外部网站页面

Html 为什么我在我的网站iframe中嵌入的外部网站会自动重定向到外部网站页面,html,iframe,Html,Iframe,如上所述,我使用普通iframe将外部网站嵌入到我自己的网站中,但当我转到嵌入外部网站的页面时,它将自动重定向到外部网站 我不希望重定向发生,有什么解决方案吗 因为代码与此类似 <script type="text/javascript"> if(top.location != window.location) { window.location = top.location; } </script> if(top.location!=windo

如上所述,我使用普通iframe将外部网站嵌入到我自己的网站中,但当我转到嵌入外部网站的页面时,它将自动重定向到外部网站


我不希望重定向发生,有什么解决方案吗

因为代码与此类似

<script type="text/javascript">
   if(top.location != window.location) {
     window.location = top.location;
   }
</script>

if(top.location!=window.location){
window.location=top.location;
}

很遗憾地说。。没有解决办法!:(

如果您可以使用ajax加载页面,那就太好了,但是由于跨域策略的原因,这是不可能的。因此,我能想到的唯一其他解决方案是使用,类似这样的方法可能会有所帮助:

/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$returned_content = get_data('http://davidwalsh.name');

下面是另一个可以在iframe上使用沙盒属性的解决方案:

沙盒属性为iframe中的内容启用了一组额外的限制

然后iframe将不再能够重定向父对象

在我的例子中,我使用带有一些值的iframe来允许脚本,并允许嵌入式站点使用相同的源

<iframe id="" sandbox="allow-scripts allow-same-origin" src="http://www.example.com/" ></iframe>


你能给我们举个例子吗?外部网站可能会有一些Javascript代码,如果它存在的话,它会在它的父窗口上执行。你可以阻止它执行。上面的链接可以显示我面临的问题的一个例子。有没有办法阻止Javascript执行?ShreejiShah是正确的,有
如果(top.frames.length>0)top.location.href=self.location;
在告诉家长重定向到自身的页面上,基本上意味着它可以作为iframe包含。我将发布一个答案作为替代。感谢Ian Jamieson真的希望解决此问题:)尝试了此代码,但没有解决此问题。。。。我对javascript很在行,很抱歉这是外部网站的代码。所以你不能编辑这个。我想说的是,这个代码在你试图在iFrame中显示的网站上。。当然,你不能编辑他们的代码。。如果你把它放在你的网站上。。然后,您的网站将阻止其他网站在其iFrame中显示。