在h5頁面中防止dns劫持的方法
通過在h5頁面的body標簽中添加以下代碼實現防止dns劫持
var del_times = 0, deTimer = null;function adGo() { var iframe = document.getElementsByTagName('iframe')[0]; if(iframe){ console.log(iframe)
//循環 iframe 父類,直到找到body和body的下一級,然后整個嵌入的代碼刪除。
var bodyNode = {tagName:''}, iframeParent, targetNode = iframe.parentNode; while (bodyNode.tagName != 'BODY'){
bodyNode = targetNode; if(bodyNode.tagName != 'BODY'){
iframeParent = targetNode;
targetNode = targetNode.parentNode;
}
} if(iframeParent) //如果iframe有父類
bodyNode.removeChild(iframeParent); else
bodyNode.removeChild(iframe);
}
del_times++; if (del_times > 10) window.clearInterval(deTimer)
}