我的老板要求提供一个不会更改的页面,以便加载两个定时弹出窗口。我找到了代码并将其编辑为我认为它应该做的事情,但它只加载了最后一个onLoad事件。我是一名设计师,我帮助制作网页,但Javascript远远超出了我的理解范围。我已经学会了如何使用单个弹出窗口并花费了很多时间来学习超时,但我似乎无法使用多个弹出功能。如果你有片刻,你会看看吗?谢谢:))
ħ
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>H's Page 1</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore, Editor -->
<!-- Web Site: The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
closetime = 3; // Close window after __ number of seconds?
// 0 = do not close, anything else = number of seconds
function Start1(URL, WIDTH, HEIGHT) {
windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
preview = window.open(URL, "preview", windowprops);
if (closetime) setTimeout("preview.close();", closetime*1000);
}
function doPopup1() {
url = "http://www.google.com";
width = 1680; // width of window in pixels
height = 1050; // height of window in pixels
delay = 10; // time in seconds before popup opens
timer = setTimeout("Start1(url, width, height)", delay*1000);
}
closetime = 3; // Close window after __ number of seconds?
function Start2(URL, WIDTH, HEIGHT) {
windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
preview = window.open(URL, "preview", windowprops);
if (closetime) setTimeout("preview.close();", closetime*1000);
}
function doPopup2() {
url = "http://www.yahoo.com";
width = 1680; // width of window in pixels
height = 1050; // height of window in pixels
delay = 5; // time in seconds before popup opens
timer = setTimeout("Start2(url, width, height)", delay*1000);
}
// End -->
</script>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<!-- Script Size: 1.27 KB -->
</head>
<body OnLoad="doPopup1(); doPopup2();">
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
</body>
</html>
答案 0 :(得分:1)
我稍微清理了你的代码,并创建了闭包来更好地处理你的settimeout 如果你必须修改它,我想你会发现以这种方式调用settimeout更容易(你会遇到更少的全局变量问题)。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>H's Page 1</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore, Editor -->
<!-- Web Site: The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var closetime = 3; // Close window after __ number of seconds?
// 0 = do not close, anything else = number of seconds
function Start1(URL, WIDTH, HEIGHT) {
var windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
var preview = window.open(URL, "preview", windowprops);
if (closetime) {
var timer = setTimeout(preview.close, closetime * 1000);
}
}
function doPopup1() {
function startCaller() {
var url = "http://www.google.com";
var width = 1680; // width of window in pixels
var height = 1050; // height of window in pixels
Start1(url, width, height);
}
var delay = 10; // time in seconds before popup opens
setTimeout(startCaller, delay * 1000);
}
function doPopup2() {
function startCaller() {
var url = "http://www.yahoo.com";
var width = 1680; // width of window in pixels
var height = 1050; // height of window in pixels
Start1(url, width, height);
}
var delay = 5; // time in seconds before popup opens
setTimeout(startCaller, delay * 1000);
}
// End -->
</script>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<!-- Script Size: 1.27 KB -->
</head>
<body onload="doPopup1(); doPopup2();">
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
</body>
</html>
答案 1 :(得分:0)
你需要一个闭包来使window.close在超时工作。如果你只使用浏览器至少在FF3.6上打开html文件,这不会起作用,但你可以把它放在你的网络服务器上。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>H's Page 1</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore, Editor -->
<!-- Web Site: The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var closetime = 3; // Close window after __ number of seconds?
// 0 = do not close, anything else = number of seconds
function doPopup(url, delay) {
var width = 1680; // width of window in pixels
var height = 1050; // height of window in pixels
setTimeout(function () {
var windowprops = "left=50,top=50,width=" + width + ",height=" + height;
var preview = window.open(url, "preview", windowprops);
if (closetime) { setTimeout(function(){
preview.close();
}, closetime * 1000); }
} , delay * 1000);
}
// End -->
</script>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<!-- Script Size: 1.27 KB -->
</head>
<body onload="doPopup('http://www.google.com', 1); doPopup('http://www.yahoo.com', 2);">
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
</body>
</html>
答案 2 :(得分:-1)
你用超时覆盖url变量。以下是在Firefox中为我工作的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>H's Page 1</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore, Editor -->
<!-- Web Site: The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
closetime = 3; // Close window after __ number of seconds?
// 0 = do not close, anything else = number of seconds
function Start1(URL1, WIDTH, HEIGHT) {
windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
preview = window.open(URL1, "preview", windowprops);
if (closetime) {
setTimeout("preview.close();", closetime*1000);
}
}
function doPopup1() {
url1 = "http://www.google.com";
width = 1680; // width of window in pixels
height = 1050; // height of window in pixels
delay = 5; // time in seconds before popup opens
timer = setTimeout("Start1(url1, width, height)", delay*1000);
}
function Start2(URL2, WIDTH, HEIGHT) {
windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
preview = window.open(URL2, "preview", windowprops);
if (closetime) {
setTimeout("preview.close();", closetime*1000);
}
}
function doPopup2() {
url2 = "http://www.yahoo.com";
width = 1680; // width of window in pixels
height = 1050; // height of window in pixels
delay = 10; // time in seconds before popup opens
timer = setTimeout("Start2(url2, width, height)", delay*1000);
}
// End -->
</script>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<!-- Script Size: 1.27 KB -->
</head>
<body OnLoad="doPopup1(); doPopup2();">
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
<p>My page text.</p>
</body>
</html>