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

时间:2017-04-18 06:11:36

标签: javascript

这是我将尝试解释的第二个问题。这是一个代码,关闭窗口3秒后我想要新的窗口english.html应该出现。谢谢

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

window.setTimeout(CloseMe, 3000);
}

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

2 个答案:

答案 0 :(得分:2)

除了用户互动之外,您无法打开新窗口。 “页面加载后三秒钟”不是用户交互,因此将被标准弹出窗口阻止规则阻止,所有现代浏览器都需要通过HTML规范来实现。

尝试重定向用户,或者更好,但不要:完全跳过三秒页。如果某些内容值得向用户展示,则值得向用户显示,直到他们点击链接为止。这样你知道他们没有注意到另一个标签,而你的内容被忽视了。

答案 1 :(得分:0)

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

&#13;
&#13;
//<![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
&#13;
/* external.css */
html,body{
  padding:0; margin:0;
}
.main{
  width:980px; margin:0 auto;
}
&#13;
<!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>
&#13;
&#13;
&#13;

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