2011-08-03 22 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は、メタタグがプロンプトページに存在すると私に伝えます。

どのような考えですか?ありがとうございます!

+0

これは何を行う予定ですか?別のページに新しいウィンドウを開いて、リダイレクトメタタグを追加しようとしていますか?なぜあなたは 'window.open'をやってみませんか? – Jacob

+0

@Jacob:顧客のリクエストに応じて、新しいウィンドウは4秒待ってから宛先ページにリダイレクトする必要があります。これは、フードの他のプロセスが完了するまでに約3秒かかります。私は、フロントエンドの動作に集中して達成するように言われました。 –

答えて

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

OK、多分私はあなたがやっていることを得る。新しいページが読み込まれている間に "リダイレクト"ページを表示する必要があります。リダイレクトを行う唯一の目的を持つまったく新しい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

ええ、私はあなたが正しいと思う、メタデータはすでに処理されています。 –

関連する問題