2016-02-03 119 views
0

所以我尝试了很多方法来自动重定向我加载到我的页面上的iframe的东西,他们似乎总是触发父窗口重新加载/重定向,而不是将iframe指向其所需的位置。window.location防止iframe重定向父窗口

我知道大多数人都想要相反的地方,当他们在iframe中完成时,他们想要重新加载父窗口或将其导向别处。但在这种情况下,我希望我的脚本继续到另一页。

是否有内置机制,不允许iframe在其内部重定向?

让为它的缘故假装代码

if(someAction === true) { 
    window.location = '/some/new/path/index_new.html?param=something'; 
} 

我也有window.top.locationwindow.parent.location尝试这样做,我认为一对夫妇的其他随机变量。包括在每个变体的末尾添加.href。如果失败,似乎iframe的父窗口总是重定向而不是实际的iframe本身,如所希望的。

的iframe是一个简单的iframe

<iframe src="/original/path/index.html"></iframe>

的脚本,将寻求重定向的iframe嵌入/original/path/index.html

+0

另一件事值得一提的是,我可以在物理点击我的'/原厂/路/ index.html'的嵌入式源链接和链接在iframe引导罚款。它只是当我试图让它自动重定向是我似乎遇到的问题或另一个问题。不料 – chris

回答

0

是的,有。请张贴您的代码或代码片段,以便我们看到您的工作内容。我为一个网站使用了一个Iframe,这个网站在同一个框架内打开了父页面的三个不同页面,并使用Cookie来回转换数据。但是,请显示一些html div和或js,以便我们看到您希望如何工作。这是我工作的一些代码。我使用本地存储

function Redirect($page) { 
     localStorage.setItem('page',$page); 
      window.location="validateorder.htm"; 

      } 

    function orderValidate($page) { 
     localStorage.setItem('page',$page); 
     $arr = $('iframe').contents().find('form').serializeArray(); 

     $.each($arr,function(i,v){ 
      createCookie(i,v); 
     }); 


     window.location="ordersummary.htm"; 
    } 

$(document).ready(function(){ 
    $('iframe').load(function() { 
     $page = localStorage.getItem('page'); 
     $subtotal = localStorage.getItem('subtotal'); 
     $tax = localStorage.getItem('tax'); 
     $total = localStorage.getItem('total'); 
     $table = $('iframe').contents().find('td#optionsPrice'); 

     $form = $('iframe').contents().find('#frmContact'); 
     $theform = $('iframe').contents().find('#theform'); 

     if($table.length > 0){ 
      $('iframe').contents().find('td#optionsPrice').text($subtotal); 
      $('iframe').contents().find('td#tax').text($tax); 
      $('iframe').contents().find('td#totalPrice').text($total); 
     } 

     if($form.length > 0){ 
      $form.find('input').each(function(){ 
       $name = $(this).attr('name'); 
       $type = $(this).attr('type'); 
       if($type != 'button'){ 
        $(this).val(readCookie($name)); 
       } 
      }); 
     }