简体   繁体   中英

In an iframe based website, how to load all iframes in a new window after user clicks on a link within one iframe?

I am having a problem with an iframe (Java servlets) based website which looks somewhat like this:

 <!DOCTYPE html> <head></head> <body> <iframe id="top" name="top" src="$link.setAction('top').relative()"></iframe> <iframe id="content" name="content" src="$link.setAction('content').relative()"></iframe> <iframe id="footer" name="footer" src="$link.setAction('footer').relative()"></iframe> </body> </html> 

Because each of the iframes is a standalone page, if a user decides to open a link from within any of the iframes, the page that is loaded in a new window or tab looks broken as it is only one part of the whole website.

I was wondering if there is a way to detect whether the user chose "open in a new tab" or "open in the new window" from the browser. Then, I would have to make sure that after this happens, all the iframes are loaded in a new window.

Let me know if there is a better solution, as well.

Use the target attribute to solve your problem when user is navigating through the iframes.

Your iframes are already named top , content , and footer . You can also place the three iframes inside of a parent iframe, which you can then name something like main_iframe .

Then for your links, specify the name of the iframe you want them to open in using the target attribute.

<a href="top.html" target="top">Example link inside the top frame which opens only in top frame</a>
<a href="content.html" target="content">Example link inside the content frame</a>
<a href="bottom.html" target="bottom">Example link inside the bottom frame</a>

<a href="main.html" target="main_iframe">Example link which opens in the parent frame</a>

You can then disable the right click event using Javascript oncontextmenu :

    window.oncontextmenu = function() {
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM