我有一个有四个链接的导航栏,点击任何链接相应的页面在导航栏下方的框架中打开,但是当我使用右键单击在新选项卡中打开时我希望打开整个页面即打开特定链接不应允许单独出现在新标签页中
链接应单独在框架中打开或显示,而不是单独显示。这可以做到吗?
<a href="page1.jsp" target="frame1">page1</a>
<a href="page2.jsp" target="frame1">page2</a>
<iframe name="frame1" src="page3.jsp">
右键单击链接并在新窗口中打开应该会再次启动整个应用程序。
最佳答案
应该只显示在 iframe 中的每个页面(page1.jsp、page2.jsp 和 page3.jsp)都应该包含 JS 以检测它是否显示在自己的窗口中。例如,如果带有 iframe 的页面是 index.jsp,则 page1.jsp 应该包含以下代码:
<script type="text/javascript">
<!--
if (top.location.href == self.location.href)
// Redirect user to main page, passing current page name:
top.location.href = 'index.jsp?frame=page1';
//-->
</script>
然后,在 index.jsp 中,您应该使 iframe 的 src 加载指定的框架源:
<%
// Establish default source:
String frame_src = 'page3.jsp';
// Establish controlled list of pages to show in the iframe (important to be restrictive)
String[] allowed_srcs = ['page1','page2','page3'];
// Get targeted frame from query string, if given:
String targeted_frame = request.getParameter('frame');
if(Arrays.asList(allowed_srcs).contains(targeted_frame))
frame_src = targeted_frame + '.jsp';
%>
<iframe name="frame1" src="<%= frame_src %>"></iframe>
关于javascript - 在 jsp 页面中管理 <iframe>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20522545/