2015-06-14 107 views
0

我想刷新新打開的窗口,我有一個鏈接,當我點擊它時,我打開一個新窗口,所以我想刷新和編輯新的窗口URL 3秒後。Jquery刷新新打開的窗口

$('.link').on('click', function(e) { 
    e.preventDefault(); 
    window.open('/page/?q=param', '_blank'); 
    setTimeout(function(){ 
    window.location.href = window.location.href.replace(/[\?#].*|$/, "/?q=new_value"); 
}, 3000); 
}); 

此代碼正在刷新原始頁面而不是新頁面。所以我想要一種方式來刷新新的。

回答

0

存儲新的窗口引用和訪問位置屬性。

var newWindow = window.open('/page/?q=param', '_blank');

你可以做這樣的事情

$('.link').on('click', function(e) { 
    e.preventDefault(); 
    var newWindow = window.open('/page/?q=param', '_blank'); 
    setTimeout(function() { 
    newWindow.location.href = newWindow.location.href.replace(/[\?#].*|$/, "/?q=new_value"); 
    }, 0); 
}); 
+1

請考慮downvote之前添加註釋 – Dhiraj

+0

非常感謝幫我 – ler

1

嘗試

// set `popup` as reference to new `window` 
var popup = window.open("/page/?q=param", "popup"); 

setTimeout(function() { 
    // set `location.href` of `popup` 
    popup.document.write("<script>location.href = location.href.replace(/[\?#].*|$/, '/?q=new_value');</script>") 
}, 3000) 
1

您的代碼將刷新原來的窗口,當然。
你要更改代碼,以便將分配一個變量:

w = window.open('/page/?q=param', '_blank'); 

的「window.location.href」將刷新當前窗口,而不是另外一個, 然後改變它的位置在的setTimeout :

w.location.href = window.location.href.replace(/[\?#].*|$/, "/?q=new_value");