2012-02-02 3 views
0

これはリレーベントの行です。javascript clearTimeoutが子ページから機能していない

// In the parent page I set the timer and attach its handle to the window 
window.warnTo = window.setTimeout("WarnTimeout();", 20000); 

// If I clear the timer in the same page it works great 
window.clearTimeout(window.warnTO); 

// In the child page launched with window.open, 
// I try to clear the timer and the timer still fires 
window.opener.clearTimeout(window.opener.warnTO); 

すべての変数が設定されているように見え、window.openerが親ウィンドウのようです。 私は困惑しています。

+0

通常、 'setTimeout'や' setInterval'に文字列引数を使うべきではありませんが、この場合はOKです。 –

答えて

2

オープナープロパティを使用してページにアクセスしようとしましたか?

window.opener.clearTimeout(window.opener.warnTO); 

これをテストするためのサンプルコードです。

Opener.html

<script type="text/javascript"> 
window.onload = function(){ 
    document.getElementById('button').onclick = function(){ window.open("opened.html");} 
    window.x = setInterval("bla()",500); 
} 

function bla() 
{ 
    var obj = document.getElementById('output') 
    obj.innerHTML += "Bla"; 
} 
</script> 

<div id="output"></div> 

<button id="button">Open the new window</button> 

Opened.html

<script type="text/javascript"> 
    window.opener.clearInterval(window.opener.x); 
</script> 

UPDATE

:私は働くことの明確な指示を与えるためにインターバルではなく、タイムアウトを使用しますあなたがコメントで指摘したようにあなたが提供したコードサンプルにも誤字があります。

warnTOからwarnToに変更してください。

+0

私はそれを取得しません。これは+2のためにコピーされた彼の正確なコード行ですか? – jAndy

+0

私はこの答えを書いた後、彼は彼の質問を更新したに違いない。 –

+0

window.opener.locationは親ウィンドウ内のURLなのでOKです。 –

関連する問題