我在使用基于iframe(Java servlets)的网站时遇到了问题,它看起来像这样:
<!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>
由于每个 iframe 都是一个独立的页面,因此如果用户决定从任何 iframe 中打开链接,则在新窗口或选项卡中加载的页面看起来已损坏,因为它只是整个网站的一部分。
我想知道是否有办法检测用户是从浏览器中选择"在新选项卡中打开"还是"在新窗口中打开"。然后,我必须确保在发生这种情况后,所有 iframe 都加载到新窗口中。
让我知道是否有更好的解决方案。
使用目标属性解决用户在 iframe 中导航时遇到的问题。
您的 iframe 已命名为顶部、内容和页脚。您还可以将三个 iframe 放在父 iframe 中,然后您可以将其命名为 main_iframe。
然后,对于您的链接,使用 target 属性指定您希望它们在其中打开的 iframe 的名称。
<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>
然后,您可以使用 Javascript 在上下文菜单上禁用右键单击事件:
window.oncontextmenu = function() {
return false;
}