在新选项卡中打开来自不同域的iframe中的链接 [英] Make links in iframe from a different domain open in a new tab

查看:127
本文介绍了在新选项卡中打开来自不同域的iframe中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是针对高级javascript程序员的!
- 适合我的英语不好:(b / b)

我们有以下情况:

- - 网站A给了我一个脚本。



---我把脚本放在网站B上(在index.html上)



---该脚本生成一个iframe,其中包含一个链接(< a>)



结果=>网站B包含一个ifame,它来自网站A



我想要做的是在新标签中打开链接:D



我可以通过添加javascript属性target =_ blank来实现此目的。但是,我
CANT选择与javascript的链接(),因为iframe来自另一个站点。浏览器不会让你访问iframe的内容,如果不是从你的网站。



我对某种黑客感到厌烦...
I正在考虑是否玩事件会帮助我?



像捕捉链接点击事件,不是停止事件,而是触发新事件以打开新选项卡中的链接。 / p>

或猫点击左键点击事件,点击右键点击事件,然后从cotext菜单中选择在新标签中打开链接(所有这些发生在用户点击链接时)



<任何建议,欢迎,我需要从不同的角度来看待这个问题... 解决方案

您可以使用代理。以下是我使用的代码:

get.php

 <?php 
//如果没有提交URL,则退出
if(!isset($ _ REQUEST ['url'])){echo'您没有指定URL'; exit();}
$ url = $ _REQUEST ['url'];
// instanciate cURL连接
$ ch = curl_init();
//设置URL
curl_setopt($ ch,CURLOPT_URL,$ url);
//这将允许您打印页面上的内容
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
//这是获取内容类型(例如图像)
curl_setopt($ ch,CURLOPT_HEADER,true);
$ res = curl_exec($ ch);
//分开标题和内容
list($ headers,$ content)= explode(\r\\\
\r\\\
,$ res,2);
//设置内容类型
header('Content-type:'.curl_getinfo($ ch,CURLINFO_CONTENT_TYPE));
//显示结果
echo $ content;
//关闭连接
curl_close($ ch);
?>

您可以通过传递链接来使用它:

  http://yourdomain.com/get.php?url=http%3A%2F%2Fotherdomain.com%2Fpage.html%0D%0A 

正如您所看到的,传递的URL应该是 <强>了urlencoded 。另外请注意,其他页面中的任何相对路径都将无法加载(您可以通过将相对路径替换为绝对路径来改善此情况)。


This question is for advanced javascript programmers! -Sorry for my bad english :(

We have the following scenario:

--- Site A gives me a script.

--- I put the script on site B (on index.html)

--- The script generate an iframe, that contains a link(< a >)

Result => Site B contains an ifame with a link in it ,from site A

All i want to do is "To open the link in new tab" :D

I can do this by adding with javascript attribute target="_blank".. BUT i CANT select the link() with javascript because the iframe is from another site. For security reasons browsers dont let you to access the content of an iframe if is not from your site.

I am loking for some sort of a hack... I was thinking if playing with events will help me ?

Like catching the event of link click, than stop the event and than fire new event to open link in new tab.

Or catch the leftclick event, stop event, fire right click event than, from the cotext menu select "open link in new tab" (all of this happens when user leftclick the link)

Any suggestions are welcome, i need to look this problem from a different perspective...

解决方案

You could use a proxy. Here is the code that I use:

get.php

<?php
  // if no URL is submitted, exit
  if(!isset($_REQUEST['url'])){echo 'You did not specify a URL'; exit();}
  $url = $_REQUEST['url'];
  // instanciate the cURL connection
  $ch = curl_init();
  // set the URL
  curl_setopt($ch, CURLOPT_URL, $url);
  // this will allow you to print the content on your page
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // this is to get the content type (for images for example)
  curl_setopt($ch,CURLOPT_HEADER,true);
  $res = curl_exec($ch);
  // seperate the headers and content
  list($headers,$content) = explode("\r\n\r\n",$res,2);
  // set the content type
  header('Content-type: '.curl_getinfo($ch,CURLINFO_CONTENT_TYPE));
  // display the results
  echo $content;
  // close the connection
  curl_close($ch);
?>

You can use it by passing it the link:

http://yourdomain.com/get.php?url=http%3A%2F%2Fotherdomain.com%2Fpage.html%0D%0A

As you can see, the URL that is passed should be urlEncoded. Also note that any relative paths in the other page will fail to load (you could improve this by replacing any relative path by an absolute one).

这篇关于在新选项卡中打开来自不同域的iframe中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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