2011-01-07 59 views
2

我的老闆已經要求一個不會更改的頁面,以便有兩個定時彈出窗口加載。我找到了代碼並將其編輯爲我原先認爲應該執行的操作,但它只加載了最後一個onLoad事件。我是一名設計師,我幫助製作了網頁,但Javascript遠遠超出我所能理解的範圍。我已經學會了如何使用單彈出並花時間學習超時,但我似乎無法讓它使用多個彈出功能。如果你有時間,你會看看嗎?謝謝:)Javascript Pop Ups

^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> 
+1

您正在混合使用程序和功能樣式。例如,變量`closetime`在全局範圍內,應該只存在一次。在函數定義之前定義它,然後在另一個函數之前重新定義它,但它不會做你認爲它的工作。順便說一句,你應該引入`var`關鍵字的局部變量。我討厭這樣說,但我不得不建議獲得一本Javascript書。在StackOverflow答案中有太多內容需要覆蓋;寫Javascript,你必須知道Javascript! – 2011-01-07 02:22:15

+0

感謝Tomalak。我將拿出第二個關閉時間。我很想了解更多,但這不是我有時間花時間學習代碼本身,希望我會有更多時間下一個項目。 – user566312 2011-01-07 02:43:01

+1

我想知道您是否可以通過談判爲非設計工作獲得更多收入! – erjiang 2011-01-07 03:48:00

回答

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

你需要一個閉包來使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>