2013-11-25 21 views
5

Xin xem cập nhật (11/27) dưới đâyshowModalDialog; Mở ra một cửa sổ mới trong IE

Tôi có một cửa sổ modal được đưa ra với nó là nội dung trong một khung nội tuyến (ASP ứng dụng webforms). Chúng ta cần phải chuyển hướng phương thức sang một trang khác, nhưng nó không thể nằm trong iframe vì lý do bảo mật (trang xử lý Paypal). Trong chế độ tiêu chuẩn của Chrome và IE, chúng tôi có mã thay đổi chính xác URL của phương thức thành URL phù hợp. Tuy nhiên trong chế độ tương thích, chuyển hướng sẽ làm cho cửa sổ phương thức mới mở bằng URL chính xác. Làm thế nào chúng ta có thể ngăn chặn nó mở một cửa sổ mới và thực sự chuyển hướng?

Đây là mã hiện tại của chúng tôi:

dialog.redirect = function (location, ignoreFrames) { 
     /// <summary>Redirects the dialog to a new URL</summary> 
     /// <param name="location" type="String"></param> 
     /// <param name="ignoreFrames" type="Boolean">If true, the dialog's URL will be changed instead of any parent frame URLs</param> 

     if (ignoreFrames === undefined) { 
      ignoreFrames = true; 
     } 

     if (ignoreFrames === true) { 
      if (window.top) { 
       //Chrome and IE9+ 
       window.top.document.location.replace(location); 
      } else { 
       //This was a supposed fix but it did not change the outcome 
       //<IE8 and compat mode 
       var redirectLink = document.createElement("a"); 
       redirectLink.href = location; 
       document.body.appendChild(redirectLink); 
       redirectLink.click(); 
      } 
     } else { 
      window.document.location.replace(location); 
     } 
    }; 

Cập nhật 11/27, với các ví dụ về vấn đề này:

Interactive Example (Yêu cầu IE10 + hoặc bất kỳ trình duyệt tốt)

Sau đây là một ví dụ về vấn đề, với tất cả mọi thứ được thiết lập như thế nào chúng tôi có nó. Khi phương thức ở chế độ tương thích với IE, nó sẽ mở một cửa sổ mới thay vì chuyển hướng phương thức. Sửa trang ở chế độ tương thích không phải là một quá trình dễ dàng, vì ứng dụng của chúng tôi dựa trên chế độ tương thích và trang phương thức bên ngoài được sử dụng rộng khắp mọi nơi. Xem trang (main.html) trong FireFox (Chrome có vấn đề bảo mật miền), nó hoạt động như mong đợi; phương thức được chuyển hướng hoàn toàn đến trang mới.

main.html

<html> 
<head></head> 
<body> 
    <a href="javascript:window.showModalDialog('modal.html', self, 'status:no;resizable:yes;help:no;scroll:no;width:1000;height:600')">Open Modal</a> 
</body> 
</html> 

modal.html

<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]--> 
<!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]--> 
<!--[if IE 8]> <html class="lt-ie9"> <![endif]--> 
<!--[if gt IE 8]><!--> <html class=""> <!--<![endif]--> 
    <head> 
     <title id="tagTitle"></title> 
    </head> 
    <body style="margin:0px">  
     <form name="Form1" method="post" action="" id="Form1"> 
      <strong>modal.html</strong><br /> 
      <iframe frameborder="1" src="frame.html" scrolling="yes"></iframe> 
     </form> 
    </body> 
</html> 

frame.html

<!DOCTYPE html> 
<!--[if lt IE 7 ]> <html class="ie6" xmlns="http://www.w3.org/1999/xhtml"> <![endif]--> 
<!--[if IE 7 ]> <html class="ie7" xmlns="http://www.w3.org/1999/xhtml"> <![endif]--> 
<!--[if IE 8 ]> <html class="ie8" xmlns="http://www.w3.org/1999/xhtml"> <![endif]--> 
<!--[if IE 9 ]> <html class="ie9" xmlns="http://www.w3.org/1999/xhtml"> <![endif]--> 
<!--[if (gt IE 9)|!(IE)]><!--> 
<html class="" xmlns="http://www.w3.org/1999/xhtml"> 
<!--<![endif]--> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width" /> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
</head> 
<body> 
<strong>frame.html</strong><br /> 
<a href="javascript:void(0)" onclick="redirectToCart();" title="My Cart">Trigger Redirect</a> 

<script type="text/javascript"> 
    var redirect = function (location, ignoreFrames) { 
     /// <summary>Redirects the dialog to a new URL</summary> 
     /// <param name="location" type="String"></param> 
     /// <param name="ignoreFrames" type="Boolean">If true, the dialog's URL will be changed instead of any parent frame URLs</param> 

     if (ignoreFrames === undefined) { 
      ignoreFrames = true; 
     } 

     if (ignoreFrames === true) { 
      window.top.document.location.replace(location); //IE will create a new window at this point, instead of changing the modal's URL 
     } else { 
      window.document.location.replace(location); 
     } 
    }; 

    function redirectToCart() { 
     redirect('anotherpage.html', true); //Change this to false to see just the inner frame's URL change 
    } 
</script> 
</body> 
</html> 

anotherpage.html

<html> 
<head> 

</head> 
<body> 
    <strong>anotherpage.html</strong><br /> 
    Success 
</body> 
</html> 
+0

Tôi có thể nhận được một số phản hồi về câu trả lời của tôi không? Điều đó có giải quyết nó cho bạn không? – Trevor

+1

Chỉ đang trong quá trình xác minh! –

Trả lời

7

vấn đề của bạn nảy sinh bởi vì bạn đang sử dụng showModalDialog trong đó giới thiệu hành vi này khi sử dụng IE.

Bạn có thể đọc về nó ở đây:

showModalDialog Opens a New Window

Nếu bạn muốn tiếp tục sử dụng showModalDialog đây là một công trình xung quanh:

modal.html

<head> 
    <base target="_self" /> 
    ... 
</head> 
<body style="margin:0px">  
     .... 
     <a href="anotherpage.html" id="go" style="display:none;"></a> 
    </form> 
</body> 

frame.html

function redirectToCart() { 
    window.parent.document.getElementById('go').click(); 
} 

Ví dụ

http://plnkr.co/edit/ClxlWqkzBmTy93kJzuru?p=preview

+1

Ah tôi đã có linh cảm nó là một cái gì đó như thế, nhưng tôi không thể tìm thấy bất kỳ bằng chứng. Cảm ơn, điều này cố định nó. –

Các vấn đề liên quan