簡體   English   中英

JavaScript彈出窗口

[英]Javascript Pop Ups

我的老板要求提供一個不會更改為具有兩個定時彈出窗口加載時間的頁面。 我已經找到代碼並將其編輯為我認為應該執行的操作,但是它僅加載了最后一個onLoad事件。 我是一名設計師,並且為制作網頁提供了幫助,但是Javascript遠遠超出了我的理解范圍。 我已經學習了如何使用單個彈出窗口,並花了一些時間來學習超時,但是我似乎無法使其與多個彈出窗口功能一起使用。 如果您有時間,可以看看嗎? 謝謝 :)

H

<!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。
如果您必須進行修改,我想您會發現以這種方式調用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>

您需要關閉以使window.close在超時中正常工作。 如果您至少使用FF3.6在瀏覽器中打開html文件,這不是行不通的,但是您可以將其放到Web服務器上。

<!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>

首先,讓我向您致以最深切的慰問,感謝您有一個老板命令您使用彈出窗口...他們很可惡:(無論如何,如果您想嚇scar訪客,我想這取決於您。

您正在使用超時覆蓋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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM