-4

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 CAN'T select the link(<a>) with JavaScript because the iframe is from another site. For security reasons browsers don't let you to access the content of an iframe if is not from your site.

I am looking 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 context 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...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Despre Femei
  • 103
  • 4
  • 2
    If a browser allows you to do this, it's a bug. Scripts from one site are not supposed to be able to affect an iframe from another site in any way. – Barmar Feb 21 '15 at 00:16
  • 1
    @GolezTrol The answes there only work if the main page and iframe are in the same domain. His problem is that they're in different domains, so that won't work. – Barmar Feb 21 '15 at 00:18
  • Hmm. Interesting. One possibility: build a proxy on your domain, so you can load the page from the other domain on your server, and feed it to the client as if it was from your own domain. You could even add the `target="_blank"` to the HTML code in the server. I think this will count as a hack. :) – GolezTrol Feb 21 '15 at 00:19
  • I am looking for a new point of view, ex: the user have the option to right click the link and open in new tab. But he wont do it, users are lazy, and many dont know this option. How can i make the user open the link in new tab without botherng it too much.. i dont want to lose the vizitor? – Despre Femei Feb 21 '15 at 00:25

1 Answers1

0

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).

Community
  • 1
  • 1
blex
  • 24,941
  • 5
  • 39
  • 72
  • I don't know the iframe src, the iframe is generated by the script given from site A when page loads... – Despre Femei Feb 21 '15 at 00:39
  • @KristoGodari Do you know **when** the iframe is generated? You could wait for that to happen, and then switch its src. – blex Feb 21 '15 at 00:41
  • Let me test it, i dont know if i can access the iframe src with js... actually the script generates 2 iframe, Iframe A, that contains Iframe B that contains the link. – Despre Femei Feb 21 '15 at 00:44
  • @KristoGodari Wait, so you have 2 nested iframes, is that right? Do they both have src's from another domain? If so, it's going to be a little more complicated than that. – blex Feb 21 '15 at 00:46
  • Yes, i have 2 iframes, from another domain :( – Despre Femei Feb 21 '15 at 00:54
  • @KristoGodari okay, then I'm afraid I won't be able to help you any further without actually seeing what these iframes contain and testing it. I'd understand if you don't want to disclose it, though. – blex Feb 21 '15 at 00:58