not able to access iframe contents using jquery or javascript


I was trying to manipulate HTML inside iframe using jquery-javascript, I am trying something like this

$(document).ready(function(){
    $('#Selector', frames['nameOfMyIframe'].document).functionStuff();
});

But I am not able to get the contents of an iframe, I get the Undefined error if iFrame is not loaded completely and when it loads I am getting permission error

How can I resolve this issue and access iframe contents using jquery?


Asked by:- neena
2
: 161 At:- 7/26/2017 7:30:39 AM
jQuery javascript iframe-contents






2 Answers
profileImage Answered by:- manish

Your issue of not able to access iframe elements using jQuery or javascript is related to same origin policy, that's why you are getting the permission error.

If the <iframe> is from the same domain, the elements are accessible using jQuery like

$("#iFrame").contents().find("#DivID").AnyFunction();

Or you can try this method which triggers after loading of iFrame gets complete

$(document).ready(function(){
//executes once the iframe has finished loading
 $("#iframeID").ready(function () { 
       $('#Selector', frames['nameOfMyIframe'].document).functionStuff();
    });
    
});

Above solution will resolve your loading issue, but it will not resolve cross-domain issue(permission error)

2
At:- 7/26/2017 8:34:32 AM
I tried the first solution and it works for me 0
By : neena - at :- 9/8/2017 7:53:35 AM


profileImage Answered by:- vikas_jk

You can try this code also

$(document).ready(function(){
    $('#IfOfFrame').load(function(){
        $('#IfOfFrame').contents().find('body').html('Your new HTML Here');
    });
});
1
At:- 8/30/2017 7:58:22 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use
Amazon Deals

Jobs from Indeed