2011-08-03 33 views
1

敬請考慮下面的代碼:爲什麼html元刷新重定向不能在提示頁面上工作?

<html> 
<head> 
     <title>This is the from page</title> 
</head> 
<body> 
    <script type='text/javascript'> 
     function redirect(destination) 
     { 
      newWindow = window.open(destination, "_blank", "width=790,height=520"); 
      newWindow.document.write("<meta http-equiv='refresh' content='4;url=" + destination + "'>"); 
      newWindow.document.write("<h1>Now redirecting to destination page in 4 seconds...</h1>"); 
     } 

     redirect("./to.html"); 
    </script> 
</body> 

基本上我只能看到提示頁面現在重定向到目的地頁面在4秒...顯示。但它永遠停留在那裏...... Firebug告訴我,meta標籤確實存在於提示頁面中。

有什麼想法?提前感謝!

+0

這是應該做什麼?你打開一個新窗口到不同的頁面,然後試圖添加一個重定向元標記到那個?你爲什麼不做'window.open'? – Jacob

+0

@Jacob:根據客戶的要求,新窗口在重定向到目標頁面之前應該等待4秒鐘,因爲其他一些進程下面的進程大約需要3秒鐘才能完成。我被告知只能專注於實現前端行爲。 –

回答

2
function redirect(destination) 
    { 
     setTimeout(function(){window.location = destination;},4000); 
    } 
1

好的,也許我明白你在做什麼;您希望在加載新頁面時顯示「重定向」頁面。如果你創建一個全新的HTML頁面,這個頁面的唯一目的就是重定向,這可能是最簡單的。重定向URL可以添加到查詢字符串中。這裏是你如何可以創建一個重定向頁面(我們假設這就是所謂的將redirect.html):

<html> 
    <head> 
    <script> 

     // Parse the query string to get the destination URL 
     var params = {}; 
     var pairs = window.location.search.substring(1).split('&'); 
     for (var i = 0; i < pairs.length; i++) { 
      var components = pairs[i].split('='); 
      params[components[0]] = decodeURIComponent(components[1]); 
     } 

     setTimeout(function() { window.location = params.redirect; }, 4000); 

    </script> 
    </head> 
    <body> 
    <h1>Now redirecting to destination page in 4 seconds...</h1> 
    </body> 
</html> 

這裏是如何你會在你的主頁使用它:

function redirect(destination) 
{ 
    window.open(
     'redirect.html?redirect=' + encodeURIComponent(destination), "_blank", 
     "width=790,height=520"); 
} 

redirect("./to.html"); 
1

我想你的代碼不工作的原因是瀏覽器已經處理了頁面及其元數據。假設是這種情況,在事實之後添加重定向將不起作用。 (誰知道更多關於瀏覽器內部的人需要驗證)

我不知道爲什麼你會運行這個而不是隻是加載頁面,但應該工作的一種方法是設置一個JavaScript超時更新彈出窗口的位置。例如:

<html> 
<head> 
     <title>This is the from page</title> 
</head> 
<body> 

    <script type="text/javascript"> 

     function redirect(destination) 
     { 
      newWindow = window.open(destination, "_blank", "width=790,height=520"); 
      newWindow.document.write("<h1>Now redirecting to destination page in 4 seconds...</h1>"); 
      setTimeout(function(){ newWindow.location=destination;}, 4000); 
     } 

     redirect("./to.html"); 
    </script> 
</body> 
+0

是的我認爲你是對的,元數據已經被處理了。 –

相關問題