简体   繁体   English

如何从 iframe 重定向到另一个 url 并在 iframe 的父级上打开它?

[英]How to redirect to another url from iframe and open it on the parent of the iframe?

I have a parent jsp called p1.jsp .我有一个名为 p1.jsp 的父级 jsp。 When i click on a link on this jsp , it opens up an iframe called iframe.jsp.当我单击此 jsp 上的链接时,它会打开一个名为 iframe.jsp 的 iframe。 Now there is a link in iframe.jsp, which when clicked it must navigate the user to another url after closing the iframe modal window, but that url must be opened on the parent session.现在 iframe.jsp 中有一个链接,单击该链接时,它必须在关闭 iframe 模式窗口后将用户导航到另一个 url,但该 url 必须在父会话中打开。 This means the parent.jsp will be replaced by the contents of the new.jsp.这意味着 parent.jsp 将被 new.jsp 的内容替换。

I fished out a code like this :我找到了这样的代码:

<script type="text/javascript">
function closeMyModal()
{
//Closing Iframe modal 
window.parent.parent.document.getElementById('ovr1').style.display = 'none';
window.parent.parent.document.getElementById('cnt1').style.display = 'none';
<%
String hamlistVal = request.getParameter("hamlistVal");
%>
//Open the new.jsp on the p1.jsp i.e. relace p1.jsp with new.jsp
window.location.href="<%=com.pack.href.getCamHref()%>&hamlistVal=<%=hamlistVal%>";

}
</script>   

My iframe modal is getting closed , but it is not navigating to the new.jsp .我的 iframe 模式正在关闭,但它没有导航到 new.jsp。 What am i doing wrong here ?我在这里做错了什么? Kindly help .请帮助。

For security purposes your iFrame will not be allowed access to the parent frame.出于安全考虑,您的 iFrame 将不被允许访问父框架。 No navigation functions are allowed.不允许使用导航功能。 If I'm not mistaken the iFrame can read the location of its parent window but cannot set this.如果我没记错的话,iFrame 可以读取其父窗口的位置,但不能设置它。

Is this JavaScript function in the iFrame or the Parent Window?这个 JavaScript 函数是在 iFrame 还是父窗口中?

Ok i got the solution to this .好的,我得到了解决方案。

I just needed to provide my full url inside the href of my iframe.jsp as :我只需要在 iframe.jsp 的 href 中提供我的完整网址:

<a href = "<%=com.pack.href.getCamHref()%>&hamlistVal=<%=hamlistVal%>"/>

Then i needed to set the attribute target as "_parent" like this :然后我需要像这样将属性目标设置为“_parent”:

<a href = "<%=com.pack.href.getCamHref()%>&hamlistVal=<%=hamlistVal%>" target="_parent"/>

This makes the iframe disappear and opens the new.jsp on the p1.jsp.这会使 iframe 消失并在 p1.jsp 上打开 new.jsp。

Hope this helps others.希望这对其他人有帮助。

I got this one when i referred this thread : Stack Thread当我提到这个线程时,我得到了这个: 堆栈线程

There is also another way to achieve this.还有另一种方法可以实现这一点。 My solution is better when you redirect to a specific page from multiple sources.当您从多个来源重定向到特定页面时,我的解决方案会更好。

In my case I just want to make sure that this page (which is my login page) would not open in the iframe.就我而言,我只想确保此页面(这是我的登录页面)不会在 iframe 中打开。 You can also add more condition if you want to do this from defined urls.如果您想从定义的 url 执行此操作,您还可以添加更多条件。

<script>
    if(window.self !== window.top)
        window.top.location.href = "<%=request.getContextPath()%>";
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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