[英]Any gotchas using typeof window.postMessage === undefined?
如果postMessage不可用,我的網頁將無法正常工作。 我想使用一個簡單的方法:
if (window.postMessage === undefined) {
//alert the user
}
這種方法是否存在不可預見的問題?
我既不是JS專家,也不是粉絲,但是這是我遇到類似問題的經驗:
我們有一個類似的問題:我們在對象上使用“ postMessage”與chrome瀏覽器中加載的本機庫進行通信。 在某些情況下,“ postMessage”在可以正常通信后仍未定義。
事實證明,在切換包含html對象的div的可見性后,調用了html對象。
我們有這樣的事情:
在我們的例子中,我們在“嵌入”對象上調用“ postMessage”(這是與本機層進行通信的方式)。
因此,如果是類似的情況,也許您已經修改了“ window”對象的某些屬性,從而使“ postMessage”功能未定義。 這只是一個提示。
function handlingMsg(e){
var heightIn = (typeof e.data === "object") ? e.data['height'] : e.data;
$("#zbFrame").attr("height",Number(heightIn));
}
//iframe 적용
// <= ie8
if (!window.addEventListener) {
window.attachEvent("onmessage", handlingMsg);
}else{ // > ie8
window.addEventListener('message', handlingMsg, false);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.