时间:2022-12-06 01:26
在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)
}