重新加载iframe并重定向父问题

时间:2014-08-21 12:02:49

标签: javascript jquery iframe

我在同一个域名,我需要通过代码登录表单并重定向到某个页面,登录会在内部服务器上进行一系列设置。

我使用iFrame实现了一个页面

  <iframe id="iframe" src="<path to login page>" width="500" height="500"></iframe>

我加载登录页面并填充id / pwd并按代码单击提交按钮,它工作正常,几秒钟后登录登录消息

  .. args have the data pulled from DB
    var $myIFrame = $('#iframe');
    $myIFrame.contents().find("input#userID").val(arg1);
    $myIFrame.contents().find("input#userPwd").val(arg2);

    $myIFrame.contents().find("input#mybutton").trigger("click");           
   ...  

如果在此浏览器中我手动键入重定向页面,则所有后端设置都会很好。它甚至在另一个浏览器选项卡上工作但是如果我按代码设置重定向的位置,我的所有登录设置都会消失。

window.location.href='<path of internal page>';

我试过延迟它不起作用

我试过等到iframe重新加载但仍会在重定向时重置所有登录内容

$("#iframe").on("load", function () {
    window.location.href= ''<path of internal page>';
    });

我能错过什么?????

2 个答案:

答案 0 :(得分:0)

当您从代码运行脚本时,它将使用IFrame执行,但如果您从控制台运行脚本,它将直接在您的主/父页面中执行。

因此,在您的代码中,您需要使用。

将重定向命令传递到您的父页面
window.top.location = '<path of internal page>';

答案 1 :(得分:0)

var page_url = '<path of internal page>';
document.getElementById('your_iframe_id').onload = function() { 
    if(jQuery("#your_iframe_id").contents().find("body").hasClass('logged-in'))
    {                   
        window.location.href = page_url;
    }                                                   
}