gpt4 book ai didi

Javascript 弹出窗口

转载 作者:行者123 更新时间:2023-11-30 06:47:51 25 4
gpt4 key购买 nike

我的老板要求一个不会更改为加载两个定时弹出窗口的页面。我找到了代码并将其编辑为我认为它应该做的,但它只加载了最后一个 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>

最佳答案

我稍微清理了你的代码,并创建了闭包来更好地处理你的设置超时。
如果您必须修改它,我想您会发现以这种方式调用 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>

关于Javascript 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com