2

I'm trying to access the iframe contentWindow property for an iframe which is hosted on a subdomain within the same domain. So I have this code on a.domain.com (which hosts the role of parent page):

<script type="text/javascript">
    $(document).ready(function () {        
        $('#frame').load(function () {            
            var iframe = document.getElementById('frame');            
            if (iframe) {
                 var item = iframe.contentWindow;             
                if (item.indexOf("Completed") != -1) {
                    window.location.href = '../Home/Completed/';                   
                } else {
                    window.location.href = '../Home/PayAgain/';
                }
            }            
        });
    });
</script>

The actual iframe content sits on b.domain.com and I want to get the current page in the iframe.

I see some examples that I need to set document.domain = "domain.com" in both a.domain.com and b.domain.com but for example what exactly do I need to set in a.domain.com and b.domain.com regarding document.domain?

In which pages in b.domain.com I need to set the document.domain value?

halfer
  • 19,824
  • 17
  • 99
  • 186
Laziale
  • 7,965
  • 46
  • 146
  • 262

2 Answers2

3

I was in this same situation and I found that using window.postMessage was a much better way to communicate between the frames: https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

If you want to use the method you mentioned, every page that loads within the frame will need to manually set document.domain on load. The parent page will also need to set it. It is just a built-in javascript property of the document object.

ramseyjacob
  • 272
  • 3
  • 11
  • I looked in postMessage functionality too, but I can't really do any changes on the file which sits in the iframe and is hosted on b.domain.com. Can you show me how did you put the postMessage functionality in both a.domain.com and b.domain.com with some dummy data of course. Thanks – Laziale Apr 07 '14 at 18:26
  • If you can't change the code in both a.domain.com and b.domain.com then you won't be able to communicate between the two. You can set document.domain UP but you can't set it DOWN. So you can change from 'a.domain.com' to 'domain.com' but you can't change 'a.domain.com' to 'b.domain.com'. You need access to the code to set the document.domain property and to do the window.postMessage method. – ramseyjacob Apr 07 '14 at 23:50
  • Can you please provide code sample how can I do that if I have access to the code, thanks – Laziale Apr 07 '14 at 23:51
  • Sorry, the code view isn't working. Hopefully you can make this out: function sendToFrame() { var mf = document.getElementById('myFrame').contentWindow; // The message that you send is up to you, as long as it is a string. I use JSON, stringify it, then parse it on the other side var jsObj = { Content: "Some Data" }; // Send the message to the target mf.postMessage(JSON.stringify(jsObj), "http://b.domain.com"); } – ramseyjacob Apr 10 '14 at 00:24
1

add document.domain = 'your.domain' in both the pages.

like this. don't forget both parent.top

document.domain = 'corp.local'; only parent like

document.domain = 'corp'; won't work.

As I've mentioned here. javascript get iframe url's current page on subdomain

this document helped. http://javascript.info/tutorial/same-origin-security-policy

Community
  • 1
  • 1
Mashhoor Gulati
  • 127
  • 3
  • 13