2

I have a child page that can be opened either from its parent page on the same domain (mydomain.com) or from other pages on other domains (as a link) such as facebook, email link etc. How can I check whether or not the child window was opened by the parent window on mydomain.com? The reason is that I want to run a jQuery code only if the window opener exists and it is on mydomain.com. Something like the following code:

if (window.opener && /*window opener is on mydomain.com*/ )
   { 
   alert('Window opener exists and on my domain');
   }
Gloria
  • 1,305
  • 5
  • 22
  • 57
  • `if(window.opener && document.referrer=='yourdomain.com')` – Ataur Rahman Munna Aug 08 '17 at 07:50
  • Possible duplicate of [How to know if window.opener comes from my webpage?](https://stackoverflow.com/questions/7100296/how-to-know-if-window-opener-comes-from-my-webpage) – Fotis Grigorakis Aug 08 '17 at 07:51
  • document.referrer doesn't work on IE when a new window is opened... – Gloria Aug 08 '17 at 07:53
  • Possible duplicate of [Check if window parent is same domain](https://stackoverflow.com/questions/19565776/check-if-window-parent-is-same-domain) – CBroe Aug 08 '17 at 07:58
  • 1
    I suggest: `if(window.opener && window.opener.location.href.indexOf('mydomain.com'))` – Tiefan Ju Aug 08 '17 at 07:59
  • 1
    (It checks for the parent window instead of an opener in that duplicate - but the principle is the same; you try to access the document, which you will only be allowed to do if the content is from the same origin. try/catch is used to catch the error this causes, if they are not from the same origin.) – CBroe Aug 08 '17 at 07:59
  • 1
    @Gloria, Yes, you are correct. In IE `document.referrer` doesn't work. – Ataur Rahman Munna Aug 08 '17 at 08:00

0 Answers0