0

I have an iframe from anoter page in another domain.:

For example:

<iframe id="myframe" src="http://stackoverflow.com/">
</iframe>

and in jQuery, i have this:

$(document).ready(function(){
    $("#myframe").load(function(){
        var obj = $("#myframe").contents().find('#hlogo');

        div.append('<img src="http://www.my-domain.com/img/myimage.jpg" />');
    });

});

But it is not adding an image. What can I do?

In this fiddle, I am trying to change the image to something else -Same problem, different sources.

Omar
  • 11,783
  • 21
  • 84
  • 114

2 Answers2

1

If you want to change the src attribute of #hlogo you need to do like so :

var iframe = $('<iframe>', {
       id:  'pay-rent',
       }).appendTo('body');


       iframe.load(function(){
        var obj = $('#pageHeaderLogo',iframe.contents());
        console.log(obj);
        obj.attr('src', 'http://www.firstchoicehousing.com/images/logo.png');
    });

iframe.attr({
 src:"http://wemanageproperties.securecafe.com/residentservices/apartmentsforrent/userlogin.asp"
 });

http://jsfiddle.net/UYPTc/
If the iframe is different domain, you cannot access the contents of it due to Same-origin policy

Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "http://wemanageproperties.reslisting.com". Protocols, domains, and ports must match.

Thom-x
  • 841
  • 1
  • 6
  • 20
-1

you can use jQuery.contents() function

docs: http://api.jquery.com/contents/

see also How to access the content of an iframe with jQuery?

Community
  • 1
  • 1
Roman M
  • 450
  • 1
  • 5
  • 12