我想在瀏覽器完全加載HTML之後用javascript打開一個新選項卡。我試過了,但它根本不起作用:如何在html完全加載後通過JavaScript打開新標籤頁?
<script type="text/javascript">document.addEventListener("DOMContentLoaded",function(event){window.open('http://www.google.com');});</script>
你能幫我指點一下嗎?
謝謝!
我想在瀏覽器完全加載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