0

I have a dynamic iframe created in my page and loading html content inside the iframe but using jquery, not able to access the html content inside the iframe.

Based on the results over the website found below two but not applicable in my case

  1. iframe is having the URL of same domain as i am using /testhtm.html in the src and loading it.
  2. iframe is getting loaded up because i have alerted when the iframe gets loaded up after that i fire my event

Getting an error in browser while on console trying to access the iframe:

Error: Permission denied to access property 'document'
document.getElementById('testiframe').contentWindow.document.body.inne...

Trying to access my div which is inside iframe:

$("#testFrame").contents().find("#myDiv");

iframe code:

var ifr = $('<iframe/>', {
    id: 'testiframe',
    name: 'genericFrame',
    src: '/testhtml.html?' + serializeForm(params),
    style: 'display: none',
    load: function(){
        alert('iframe loaded !');
    }
});
$('#overlay').html(ifr); // Attaching it to overlay so that iframe loads in a lightbox

Any suggestions or tricks to actually access the div and show it?

UPDATE:

I am getting something using javascript:

var ifrBody = document.getElementById('testiframe').contentWindow;

returns me window object with content but i am unable to access it

but using jquery it throws error:

 var iBody = $("#testFrame").contents().find("body");

Error: Permission denied to access property 'document'
GOK
  • 2,338
  • 6
  • 34
  • 63
  • Possible duplicate of [this](http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe). Have check it? If yes, please update your question, with why that is not suitable for your case. – Athafoud Aug 05 '14 at 06:58
  • Did you try to read this? The same question here !! http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe – BlackPanther Aug 05 '14 at 07:05
  • You can not read the content of iframe like this.. You need to implement it in another way like message pass – Devendra Soni Aug 05 '14 at 07:14
  • How @DevendraSoni? Can you please explain with an example or link to it? – GOK Aug 05 '14 at 07:17
  • @GOK what exactly you want to do? You can also use jQuery load and so on – Moons Aug 05 '14 at 07:25
  • @Moons I want to access it via jquery preferrably if not then javascript. I dont want to do it on jQuery load(), i want to do it after load() in some other click event of a tag or button' – GOK Aug 05 '14 at 07:40
  • What @Moons said is that you can call load(), and load the page inside the current page, instead of doing an iframe. You could do $.load("testHtml.html", ...), then access its content. – Larta Aug 05 '14 at 07:43
  • I am setting the src to fill the iframe rt? – GOK Aug 05 '14 at 07:47

0 Answers0