AFAIK we cannot access the DOM content of iframe which has been loaded from different domain. But in my case I have a website with below name
http://dom1.myMainDomain.com/******
In that page I am loading one HTML file from sub-domain into an Iframe in above website which is sub-domain of as above. i.e
http://dom2.myMainDomain.com/******/1.html
I have tried below code but it is not working
$(document).ready(function () {
var str = "http://dom1.myMainDomain.com/*****Images/";
$('body').append('<iframe id="ifr" style="position:absolute;top:0px;left:0px;width:100%;height:100%" src="' + str + $('#htmNum').val() + '.html"></iframe>');
document.domain = "myMainDomain.com";
$('#ifr').load(function () {
$('#ifr').contents().find('body').html('Hey, i`ve changed content of <body>! Yay!!!');
});
});
But it did not work for me.. and The web site where I am embedding the above code is
http://dom2.myMainDomain.com/*****.aspx
Please help me on this. As per the answer I have tried and it was working in IE7./ But in Chrome I can see permission denied message
You must do
document.domain = "myMainDomain.com";in both paretnt page and herehttp://dom1.myMainDomain.com/*****Images/. Your document ready function is Ok, but you also should have similar piece of code in herehttp://dom1.myMainDomain.com/*****Images/. I would place it in head section likeAnd suppose it would be better to move put the same code in head of parent document. Just to be sure that everything is set correctly in when you access an iframe
Yeah. Just double checked it. I can access an iframe content with code like this:
if in both parent window and iframe there is
document.domain = "com.local";in javascript