window.addEventListener (ข้อความไม่ทำงานกับ IE11

ฉันกำลังเปิดป๊อปอัปและอันนี้ส่งข้อความโพสต์ไปยังผู้เปิด ฉันได้เพิ่ม ListenerEvent ลงในหน้าต่างหลักสำหรับ 'ข้อความ' แต่ผู้ฟังนี้ไม่เคยถูกเรียกใน IE 11 มันใช้งานได้กับ firefox

ฉันพยายามรอหน้าต่างหรือเคล็ดลับในการแทนที่ eventListener ด้วย setInterval แต่ฉันไม่สามารถเข้าถึงข้อมูลของเหตุการณ์ได้ในกรณีนี้ และฉันได้ตรวจสอบกระทู้ทั้งหมดที่คล้ายกับคำถามของฉันแล้ว ดังนั้นฉันจึงลองตัวอย่างง่ายๆ เล็กๆ น้อยๆ เพื่อตรวจสอบว่า 'ข้อความ' ของ addEventListener ใช้งานได้กับ IE11 แต่ไม่ได้เป็นเช่นนั้น

สคริปต์ในหน้า html หลักของฉัน:

var popup = window.open("popup.html", "Connection", 
                'toolbar=no, location=no, directories=no, menubar=no, status=yes, scrollbars=no, resizable=yes, copyhistory=no, '
                + 'width=' + w + ', height=' + h + ', top=' + y + ', left=' + x);
popup.postMessage("The user is 'bob' and the password is 'secret'",
                  "*");
                  },500);

สคริปต์ในหน้าป๊อปอัป html ของฉัน:

function receiveMessage(event)
{
    alert("OK popup");
    console.log("djedjeidjeidjiejdie");
}
window.addEventListener("message", receiveMessage, false);

สำหรับฉัน ผลลัพธ์ควรเป็นหน้าต่างแจ้งเตือนที่ปรากฏขึ้นเมื่อเปิดป๊อปอัป นี่เป็นกรณีของ Firefox แต่ไม่ใช่กับ IE11 ไม่เข้าใจว่าทำไม


person JPG    schedule 26.03.2019    source แหล่งที่มา
comment
จะเกิดอะไรขึ้นถ้าคุณรอให้ป๊อปอัปโหลดขึ้นมา? popup.onload = function() { popup.postMessage(...   -  person Kaiido    schedule 26.03.2019


คำตอบ (1)


ลองทำการทดสอบด้วยโค้ดด้านล่าง มันทำงานร่วมกับ IE 11

รหัสหน้าหลัก:

<html>
<head>
    <title>Page Title</title>
    <script>
    //create popup window
var domain = 'http://example.com';
var myPopup = window.open(domain + '/HTML/popup_page.html','myWindow');

//periodical message sender
setInterval(function(){
    var message = 'Hello!  The time is: ' + (new Date().getTime());
    console.log('blog.local:  sending message:  ' + message);
    myPopup.postMessage(message,domain); //send the message and target URI
},6000);

//listen to holla back
window.addEventListener('message',function(event) {
    if(event.origin !== 'http://example.com') return;
    console.log('received response:  ',event.data);
},false);
    </script>
</head>
<body>

<h1>Main page</h1>

</body>
</html>

รหัสหน้าป๊อปอัป:

<!Doctype html>
<html>
<head>
<script>
window.addEventListener('message',function(event) {
    if (event.origin !== 'http://example.com') return;
    alert("OK popup");
    console.log('message received:  ' + event.data,event);
    event.source.postMessage('holla back youngin!',event.origin);
},false);
</script>
</head>
<body>
<h1>popup_page</h1>
</body>
</html>

เอาท์พุตใน IE:

ป้อนคำอธิบายรูปภาพที่นี่

ป้อนคำอธิบายรูปภาพที่นี่

โปรดทราบว่าคุณจะได้รับการแจ้งเตือน () ในขณะที่คุณเรียกใช้โค้ด

อ้างอิง:

window.postMessage API ของ HTML5

person Deepak-MSFT    schedule 27.03.2019
comment
สิ่งนี้ใช้งานไม่ได้เพราะฉันใช้เส้นทางไปยังไฟล์คงที่ในเบราว์เซอร์: C://..../main.html ถ้าฉันเรียกเพจผ่านเซิร์ฟเวอร์ 'main.html มันจะทำงานใน IE 11 - person JPG; 27.03.2019