2

Hi guys I have created these two pieces of code in JSFiddle: First code and Second code. If you take a look at the second code, I am trying to close the iframe when a url in the iframe is clicked.

Thank you in advance

Astad
  • 193
  • 10

2 Answers2

0

It looks like you do not have CORS access, so you can't click on anything on the <iframe src='page'> using JavaScript.

$(document).ready(function(){
  var ifc = $('#iframecontainer'), ifr = ifc.find('iframe');
  var btn = $('#button');
  ifc.fadeIn('slow', function(){
    ifc.css('background', "url('http://www.calgaryramsrugby.com/images/ajax-loader.gif')");
    ifr.attr('src', 'http://coinurl.com/get.php?id=22615').load(function(){
      ifc.css('background', '#fff');
      btn.click(function(){
        ifc.hide();
        alert("Can't find attributes in iframe through JavaScript without CORS access");
      });
    });
  });
  btn.mouseover(function(){
    btn.css('cursor', 'pointer');
  });
});

If it hides your Element it won't open the link inside the iframe. http://jsfiddle.net/PD82A/4/

StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • Hi PHPglue, can you please edit the jsfiddle and post the link, thank you. – Astad Mar 11 '14 at 23:51
  • Hi PHPglue, this is a good start however I want the advert to open, not the source of the advert. Also after you click on the advert the button should go away as well. – Astad Mar 15 '14 at 19:33
  • Hi PHPglue, I what I meant by 'I want the advert to open' is I want it to open in a external window, then I want the Iframe to go away and another url to open which is www.tanktrouble21.tk – Astad Mar 16 '14 at 00:40
  • Yes, I understand. The problem is that, although you do have URL http://coinurl.com/get.php?id=22615, which loads your iframe and contains links, you don't actually have access to the content in the iframe itself, so you can't access any link/s with JavaScript. I repeat, you can't grab link information from the content which loads into the iframe, or execute any JavaScript on it without CORS access. Events on your page happen before you click on the content that loads into your iframe. You can't fire a click Event on the iframe `src` of someone else's page without CORS access. – StackSlave Mar 17 '14 at 21:04
0

you're not allowed to access parent.document in this case of a iframe without using a workaround.

you get a CORS error because of the security settings.

SecurityError: Blocked a frame with origin "http://fiddle.jshell.net" from accessing a cross-origin frame.

The workaround works because you include another iframe in your iframe that comes from the same origin as the parent and thus gives you access to the main window

Community
  • 1
  • 1
Ben
  • 13,297
  • 4
  • 47
  • 68