How force iFrame open all links stay in the iFrame

2019-07-01 13:27发布

问题:

I'm hosting an iFrame and it seems like when a link points to external domains it loads it on the main window and not in the iFrame. Is there a way to force the links to be opened in the same iFrame?

Notice: I can add anything I want to the page loaded in the iFrame (using Chrome extension).

I tried adding:

<base target="_parent" />

But it didn't do any good...

回答1:

Check Headers of your external domain page. If X-Frame-Options Header is set to DENY, browser ignores loading content to iframe or frame set and loads in main frame of browser.

Regardless of the site attempts the page cannot be displayed in a frame.

Your Code

<base target="_parent" /> is used for Loading the result into the parent browsing context of the current one. If there is no parent, this option behaves the same way as _self, so it can not solve your problem

Use <base target="myframe"> instead where myframe is name of your iframe.

<iframe width="200" height="200" name="myframe"></iframe>

Demonstration

Look for server headers for loading pages

<html>

    <head>
        <base target="myframe">
    </head>

    <body>
        <a href="hi.html">Base</a>
        <a href="bye.html">A Tag</a>
        <iframe width="200" height="200" name="myframe"></iframe>
    </body>

</html>

References

  • X-Frame-Options Header