2012-01-13 17 views
1

我發現了以下代碼,我想知道如何在更改newPage之前重複此代碼9次。我現在正在做的是製作10個html文檔,並且我將newPage更改爲page2.html,page3.html,page4.html,因此計數完成後,它會更改並最終遍歷所有這些html文檔。我想保留它只有2個文件。 index.html與此代碼和end.html。索引虐待執行此代碼9次然後更改爲end.html。誰能幫忙?如何用java腳本多次重複一個進程

var startTime = 45; 
var newPage = "page2.html"; 

function countDown() { 
    startTime--; 
    document.getElementById("counter_display").innerHTML = startTime; 

    if (startTime == 0) { 
     window.location = newPage; 
    } 
} 

function gett(id) { 
    if (document.getElementById) { 
     return document.getElementById(id); 
    } 
    if (document.all) { 
     return document.all.id; 
    } 
    if (document.layers) { 
     return document.layers.id; 
    } 
    if (window.opera) { 
     return window.opera.id; 
    } 
} 

function watchNow() { 
    if (gett('counter_display')) { 
     setInterval(countDown, 1000); 
     gett("counter_display").innerHTML = startTime; 
    } else { 
     setTimeout(watchNow, 50); 
    } 
} 

document.onload = watchNow(); 

<p><b id="counter_display"></b></p> 
<iframe frameborder="no" height="735" src="http://website.com/video.php" width="385"></iframe> 
+0

你想要什麼是JavaScript即使你已經改變了頁面仍然存在?這是不可能的。你可以使用Ajax將頁面內容加載到另一個框架中? – 2012-01-13 14:25:39

+0

我不認爲你需要使用除'getElementById'之外的任何其他內容 – qwertymk 2012-01-13 14:42:03

回答

1

如果你加載與AJAX其他頁面的內容插入到當前頁面的框架,那麼你可以用替換最後一行:

document.onload = function() { 
    for (var i= 1; i < 10; i++) { 
     newPage = "page"+i+".html"; 
     watchNow(); 
    } 
} 

但至於你怎麼做第一位,我需要更多關於整個頁面的功能等信息。

+0

是的,我在所有10頁上都有此代碼。在這個頁面上有一個iframe,當計數器不爲0時,它會轉到下一頁。我想消除9個頁面,只需要這個頁面循環9次,然後加載end.html – bammab 2012-01-13 14:30:54

相關問題