Michael Schwartz
Michael Schwartz

Reputation: 8415

Check if IFrame is Embeded on another site

I know about document.referrer and window.location however I would like to find out in this example if http://somesite.com/page (url src in iframe) would either console.log or document.body.innerHTML to tell the user on http://whatever.com/

"iFrame is on another site"

or

"iFrame is on the main domain"

Is this at all possible in JavaScript?

Upvotes: 0

Views: 168

Answers (2)

Michael Schwartz
Michael Schwartz

Reputation: 8415

I found out what I want to do is not possible because of the the Same Origin Policy :(

Upvotes: 1

osmanraifgunes
osmanraifgunes

Reputation: 1468

I think a recursive function can be useful. If the framed page is cotrolled by you something like this can work.

Create an element inside it and make id attribute mycontrolelemet

Then

function controller(element)
{
   if( element. nodeName == 'iframe' )
      {
          alert("it is a frame");
      }
    else
      {       
           controller(elemet.parentNode);
       }
}

controller(document.getElementById("mycontrolelemet").parentNode);

Upvotes: 0

Related Questions