如何在html完全加载后通过JavaScript打开新标签页?
问题描述:
我想在浏览器完全加载HTML之后用javascript打开一个新选项卡。我试过了,但它根本不起作用:如何在html完全加载后通过JavaScript打开新标签页?
<script type="text/javascript">document.addEventListener("DOMContentLoaded",function(event){window.open('http://www.google.com');});</script>
你能帮我指点一下吗?
谢谢!
答
您可以只设置一个函数作为window.onload
值:
window.onload = function() {
window.open('http://www.google.com')
}
答
当最初的HTML文档 完全加载和解析的DOMContentLoaded事件被触发,而无需等待样式表, 图像,和子帧完成加载。
使用load
事件
答
<script>
document.addEventListener("DOMContentLoaded", load, false);
function load(){
window.open("https://www.w3schools.com", '_blank');
}
</script>
Add '_blank' if you need new tab. And if you facing problems because of the browser ,this link might help : https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript.
Hope this helps
答
AutoExecutaable功能 在autoexecutable功能你不需要的事件或鼠标事件,因为浏览器自动执行功能,所以这样对执行功能:
(function(w){
w.open('http://www.google.com');
//or you can use the following instead window.open
w.location.href ="http://google.com";
})(window);
弹出式窗口拦截器不会允许它。 – epascarello