Tabsurface Add to Chrome

← Blog

“Refused to connect”: why a website won’t load in an iframe

Updated

You put an address into an <iframe> and get a grey box that says “example.com refused to connect.” The same address opens normally in a tab. Nothing is broken, and the site is not down. The site has told browsers not to show it inside other pages, and your browser did as it was told.

What you can do about it depends on who you are: the owner of that site, a developer embedding somebody else’s site, or a person who wants a few sites side by side on their own screen. This guide takes the three in turn. We make Tabsurface, a Chrome extension that belongs to the third case, and we say where it fits.

Short answer

  • The cause is a response header: X-Frame-Options, or Content-Security-Policy with a frame-ancestors rule.
  • The page doing the embedding cannot override it. No iframe attribute and no script switches it off.
  • Your own site: change the header on the server.
  • Somebody else’s site: use their official embed or their API, or link to it.
  • Your own screen: a browser extension can lift the rule for you. Pick one that limits where it does so.

What the browser is telling you

Chrome and Edge show “refused to connect” in the frame. Firefox shows “Firefox Can’t Open This Page”. The precise reason is in the console of the developer tools (F12, then Console), in one of two forms:

Refused to display 'https://example.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.
Refused to frame 'https://example.com/' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self'".

The first names the older header and the second the newer one. They do the same job.

The two headers

Header Value Who may show the page in a frame
X-Frame-Options DENY Nobody
SAMEORIGIN Only pages of the same site
Content-Security-Policy frame-ancestors 'none' Nobody
frame-ancestors 'self' Only pages of the same site
frame-ancestors 'self' https://app.example.com The same site and the listed addresses

frame-ancestors is the newer of the two and the only one that can name specific sites. When a response carries both, current browsers follow frame-ancestors and ignore X-Frame-Options. Most sites still send the older header as well, for older browsers. An old third value, ALLOW-FROM, is no longer supported by current browsers.

Both exist to stop clickjacking. In that attack a hostile page loads a real site in an invisible frame and lays it over its own buttons, so a click on “Play” lands on “Delete account” or “Confirm payment” in the site underneath. A site that refuses to be framed can’t be used that way. That is why nearly every product you sign in to sends one of these headers.

How to check what a site sends

  1. Open the page in an ordinary tab and open the developer tools (F12, or ⌥ ⌘ I on a Mac).
  2. Go to the Network tab and reload the page.
  3. Click the first request in the list, the page itself, and look under Response Headers for x-frame-options and content-security-policy.

From a terminal, one command does the same for a public page:

curl -sI https://example.com | grep -iE "x-frame-options|content-security-policy"

Check the address you actually want to embed. Many sites allow framing of their public pages and forbid it for everything behind the login.

If it is your own site

Then the fix is yours to make, on the server. Decide which sites may frame yours and say so with frame-ancestors:

Content-Security-Policy: frame-ancestors 'self' https://app.example.com

Then find what adds the old header, because it is often not your own code. Django’s clickjacking middleware sends X-Frame-Options: DENY by default, Rails sends SAMEORIGIN, and so does the Helmet package for Express. A CDN or a hosting platform in front of the application can add its own. Allow only the addresses that need it. Allowing everyone brings the clickjacking problem back.

If you are embedding somebody else’s site

You cannot override the rule from your page. There is no iframe attribute for it, the sandbox attribute only restricts a frame further, and your scripts can’t reach into a response from another site. What is left:

One route to avoid is a proxy that fetches the site and strips the headers. The page then arrives from your address, not the site’s, so its logins and cookies no longer apply, its own defences are off, and you are very likely breaking its terms of use.

If you just want the site on your own screen

This case is different, because the rule is enforced by your own browser, on your own computer. A web page can’t change how the browser treats a response, but a browser extension can: it is allowed to remove those two headers before the browser acts on them. Two things are worth checking before you install one.

Where it lifts the rule. Some extensions remove the headers from every site in every tab. That switches clickjacking protection off for all of your browsing, including the tab with your bank in it. An extension should limit the change to the one page where you asked for it.

What happens after the page loads. The headers are only the first of three obstacles:

  1. The headers, as above. Without them handled you get the empty box.
  2. Cookies. Browsers send most cookies only when the site is the page in the tab. In a frame on another site’s page they are left out, so the page loads but comes up signed out, or signed in and unable to save.
  3. The site’s own scripts. A page can notice that it is not the top window and refuse to work. No header is involved, and nothing outside the page can prevent it.

Tabsurface is our answer to this case. It adds a canvas to Chrome on which sites sit side by side as cards. It removes the two headers only for frames inside its own canvas tab, and every other tab behaves exactly as before. Pages on the canvas get the same cookies as in a tab, so a site you are signed in to opens signed in. The third obstacle remains: Google’s sign-in pages, banks and sites with strict bot protection can tell they are in a frame and decline, and for those every card has an “Open in tab” button. How the other approaches compare is covered in Split-screen extensions for Chrome.

Other reasons an iframe stays blank

Questions

What does “refused to connect” mean in an iframe?

The site sent an X-Frame-Options header or a Content-Security-Policy: frame-ancestors rule that forbids showing it inside another page, and the browser obeyed. The site is working. It has declined to be framed.

Can I bypass X-Frame-Options from my own page?

No. The rule is applied by the visitor’s browser to the response of the other site, and nothing in your HTML or JavaScript can change it. Only the owner of that site can change the header. A browser extension can remove it, but only in the browser of the person who installed the extension.

Why does the site open in a tab but not in an iframe?

The headers apply only to framing. In a tab the site is the top page and no rule is broken. In a frame it is inside somebody else’s page, which is exactly what the header forbids.

Is X-Frame-Options deprecated?

It has been superseded by the frame-ancestors rule of Content Security Policy, which can also list the sites that are allowed. Browsers still honour X-Frame-Options, and most sites send both. When both are present, current browsers follow frame-ancestors.

The page loads in the iframe, but I am signed out. Why?

Because of cookies. By default a browser sends a site’s cookies only when that site is the page in the tab. In a frame on another site’s page, the session cookie is left out and the site treats you as a stranger.

Is it safe to remove these headers with an extension?

It depends on where the extension removes them. An extension that strips them in every tab turns clickjacking protection off for all of your browsing. One that limits the change to its own page, showing sites you put there yourself, leaves the rest of the browser protected. Tabsurface removes the headers only inside its canvas tab.

Related guides