title: Domain protected iframe
body: <p>I am using this code on a page with a video stream to prevent people from iframe it:</p>
  
  <pre><code>&lt;script&gt;if(self != top) { top.location = self.location; }&lt;/script&gt;
  </code></pre>
  
  <p>but now i would like to allow one or more domains to embed the page using iframe. I looked on stackoverflow and found this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  if (window.top.location.host != "website.com") {
  document.body.innerHTML = "Access Denied";
  }
   &lt;/script&gt;
  </code></pre>
  
  <p>But it doesn't work. I know there must be a way because i remember 2 years ago a website that had something like this and when i tried to embed their page with iframe, it redirected me to an adult website and the iframe only worked on their domain.</p>
  
  <p><strong>Looks like i solved the problem</strong></p>
  
  <pre><code>&lt;script&gt;
  if (top.location.host != "example.com") {
      window.location.href='http://example.com/redirected';
  }
  &lt;/script&gt;
  </code></pre>