繁体   English   中英

3秒后在同一选项卡中打开一个新窗口

[英]Open a new window after 3 seconds in same tab

嗨,这是我要解释的第二个问题。 这是一个代码,它会在3秒后关闭窗口,我希望新窗口english.html出现。 谢谢

<html>
<head>
<script>
function loaded()
{

window.setTimeout(CloseMe, 3000);
}

function CloseMe() 
{
  window.close();
 }
</script>
</head>
<body onLoad="loaded()">
Hello!
</body>

您无法打开新窗口来响应用户交互以外的任何其他情况。 “页面加载后三秒钟”不是用户交互行为,因此将被HTML规范要求所有现代浏览器实现的标准弹出窗口阻止规则所阻止。

尝试重定向用户,或者更好的是,不要:完全跳过三秒钟的页面。 如果有什么值得向用户展示,那么值得给用户展示,直到他们单击链接为止。 这样一来,您就知道当您的内容被忽视时,他们并没有将注意力转移到另一个选项卡上。

以下是如何实现所需目标的简单示例:

 //<![CDATA[ // external.js var doc, bod, htm, C, E, T; // for use on other loads addEventListener('load', function(){ // load start // I threw in a few goodies to study - it will help you later doc = document; bod = doc.body; htm = doc.documentElement; C = function(tag){ return doc.createElement(tag); } E = function(id){ return doc.getElementById(id); } T = function(tag){ // returns an Array of Elements by tag name return doc.getElementsByTagName(tag); } // notice that `window` is implicit, so you don't actually need to use it to access its properties setTimeout(function(){ // unexecuted functions and Anonymous functions behave the same location = 'https://www.w3.org'; // it's really this easy to set the `window.location.href` }, 3000); });// load end 
 /* external.css */ html,body{ padding:0; margin:0; } .main{ width:980px; margin:0 auto; } 
 <!DOCTYPE html> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> <head> <meta http-equiv='content-type' content='text/html;charset=utf-8' /> <link type='text/css' rel='stylesheet' href='external.css' /> <script type='text/javascript' src='external.js'></script> </head> <body> <div class='main'> <div>Just a very simple example</div> </div> </body> </html> 

注意:您应该练习使用外部源,以便可以对其进行缓存。 只要确保将文件名和路径更改为以后更改的任何源即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM