Метод window.top.postMessage Перенаправляется на начальную страницу - PullRequest
0 голосов
/ 23 мая 2019

Привет, у меня есть две страницы в iframe (index.html, another-one.html). index.html имеет кнопку, которая перенаправлена ​​на another-one.html один раз another-one.html отправил сообщение, используя window.top.postMessage, в результате чего оно перенаправилось обратно к исходному.

  1. index.html (страница загружена изначально)
  2. другой-page.html.
  3. Триггер window.top.postMessage с другого-page.html
  4. Перенаправлено на начальную страницу.

Ожидается, что должен остаться в index.html

Страница хоста или страница контейнера, загруженная с помощью index.html

код страницы хоста

  <iframe src ="http://127.0.0.1:8888/index.html" name="example" id="example" application="yes">
    </iframe>

код js на странице хоста

window.addEventListener("message", (e)=>{
  if(e && e.data){
    this.pagedata  = e.data;
  }
}, false);

код index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>TEST1</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
  <script type="text/javascript">
    $(document).ready(()=>{
        $("#redirectButton").click(()=>{
            window.location.href="./another-page.html";
        });
    })
  </script>
</head>
<body >
    <button id="redirectButton" type="button">Redirect</button>
</body>
</html>

код another-page.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>New Page</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
  <script type="text/javascript">
    $(document).ready(()=>{
        $("#btnMessage").click(()=>{
            let obj = {
                message:"this is new message"+(new Date())
            };
            window.top.postMessage(obj, '*');
        });
    })
  </script>
</head>
<body >
    Another Page
    <input id="btnMessage" type="button" value="Send Again"></input>
</body>
</html>
...