36

I'm making web page that has to show another domain's web page.

For example, in my web html, there are two <div> tags.

Like :

<html>
<head></head>
<body>
   <div>
      <p> hello world </p>
   </div>
   <div>
      <!-- other domain's web page comes here -->
   </div> 
</body>
</html>

To resolve my issue, I should use <iframe>, <embed> or <object> tags, but this causes the cross domain problem. So, I can't use it to show other domain's web page. They doesn't allow me to use it.

So, for example :

<iframe src="http://stackoverflow.com"></iframe>

It doesn't work. My web page can't show stackoverflow.com. Because, stackoverflow denies this.

I have searched lots of things with these issues. But they are just ajax or JSONP. It was not iframe.

Are there solutions to resolve my problem?? Or is it impossible to implement this?

user7159879
  • 637
  • 2
  • 8
  • 14

2 Answers2

34

You need control over the domain you want to embed to remove/amend its CORS policy.
If the domain has explicitly blocked Cross-Origin requests, there's nothing you can do about it.

This is used to avoid anyone hijacking any site you want (you could have a full screen Google in an iframe running with your ads on top on bettergoogle.com, things like that).

This page will give you more insights on Cross-Origin

vsync
  • 118,978
  • 58
  • 307
  • 400
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • 1
    Ok, we have control over the domain to be embedded. How to specify CORS policy for it to work? – Marius Jul 28 '20 at 08:00
  • 1
    useful to mention: Unless specifically restricted, by default iframe embedding 3rd party sites works. – Marius Jul 28 '20 at 08:38
  • 1
    "CORS does not apply when attempting to programmatically access content from a cross-origin iframe. If you want to access content from an iframe on a different domain, you will need to make use of the Web Messaging API" -- https://stackoverflow.com/a/22413275/2603297 – Steven Schkolne Aug 22 '21 at 03:55
  • The word **"programmatically"** is doing all the heavy lifting in @StevenSchkolne 's quote. This question isn't (necessarily) about that, so it doesn't necessarily apply. – Daniel Kaplan May 17 '23 at 23:26
8

If you have the permission of the owner of the domain in the iframe, you can ask them to add your domain to their cross-origin policies so you can do this.

If you don't have permission to show their content on your site, I'm happy to say that modern browsers do not support such unethical behaviour, and there is no way of doing what you are trying to do.

daveyfaherty
  • 4,585
  • 2
  • 27
  • 42