[英]Reload tab content in browser
如何重新加載特定的瀏覽器標簽內容? 我搜索不能僅在JQuery中使用的解決方案(如果有更多解決方案,我會選擇它)。
我需要這個:
我在另一個頁面上有很多鏈接的頁面(鏈接格式: http : //domain_name.cz/?still_same_text=1_1_1_1_1_1_1386662409 ;另一個頁面具有此鏈接: http : //domain_name.cz/?still_same_text=1_1_14_1_2_1387231396等。每個人鏈接的格式僅與第一個數字相同)。 當我第一次單擊某個鏈接時,我會加載新標簽頁(對於此文本,我說他只是“ tab2”),如果我再次單擊另一個鏈接,則會重新加載此“ tab2”(=我從以下位置更改此“ tab2”中的網址)當前為新=>我在此標簽中加載新的內容)。
有什么辦法嗎?
我嘗試使用該主題的解決方案, 但這對我不起作用,我的意思是我遇到了難題。 我正在研究此主題中的解決方案,但這是另一個問題。 有1個鏈接,但是我可以有更多鏈接,並且在JS中有鏈接,但是我需要在PHP中生成它們。
我以前沒有做過,但是這個問題似乎很有趣。 因此,如果您提到的答案不起作用,那么我將嘗試以下概念,它可能不是最好的,但這是有的。
1-在服務器上(正確地在會話中)創建一個跟蹤器,以跟蹤用戶打開的頁面。 因此,每當用戶打開頁面時,都會發出ajax請求,以發送帶有隨機生成的ID號的頁面網址,以供以后存儲和檢查。 每個標簽將獲得不同的ID。
2-在所有等待服務器發出命令的頁面中添加javascript偵聽器。 根據服務器的響應,這些偵聽將執行以下操作:
沒什么->第一次打開頁面的情況。
關閉選項卡 ->如果打開了具有相同鏈接的新選項卡。 因此,保留新標簽並關閉舊標簽。 //還是關閉新的並刷新舊的?
Update- >更新服務器,一旦打開選項卡(直接在頁面加載時),並且在關閉之前 ,請發送頁面URL和當前選項卡ID以便在服務器上進行檢查。
繼續檢查服務器,如果打開了具有相同URL的較新選項卡,則關閉當前窗口。
我不確定該解決方案是否適合您,所以我只會編寫一個偽代碼。 如果適用,那么以后可能會有所改善。
偽代碼php:tabControl.php
//session array structure
//when a new tab is added, it will take the following index of current url array. So later we can know which one is
//$_SESSION["tracker"][ current url ][ tab id] newer id will be added to the array in an ascending order, not based on their value, so later we can check which tabId came later.
$action = $_POST["action"];
$tabId = $_POST["id"];
$tabUrl = $_POST["url"];
if($action === "check")
{
processTab(tabUrl , $tabId);
}
elseif($action === "closing")
{
closeTab(tabUrl , $tabId) ;
}
/*if this a second tab opened with the same url, then echo the previous tab id and command the client to close self.
*if this the first tab, then store it in session.
*/
function processTab($tabUrl , $tabId)
{
//if new, then pass it to addTab
// else, check if there is a newer tab opened, then call closeTab
}
function addTab($tabUrl , $tabId)
{
// add a new tab associated with tabUrl -> couter -> tabId
}
function closeTab($tabUrl , $tabId)
{
//set that $tabUrl with the id to null.
//and ask the client to close the tab. //echo json_encode(array("responce"=>"closeSelf"))
}
在客戶端,您可以使用類似於以下偽代碼的內容:
//current tab id
var tabId = Math.floor((Math.random()*100)+1);
//On exit, just run once before user close the tab/window.
$(window).unload(function(){
$.ajax({
type: 'POST',
url: 'tabControl.php',
async:false,
data: {"url":window.location.pathname , "action":"closing" , "id":tabId}
});
});
// keep checking periodically
function checkTabs()
{
//On load, just run once
$.ajax({
type: 'POST',
url: 'tabControl.php',
data: {"url":window.location.pathname , "action":"check", "id":tabId},
type:"json",
success:function(data)
{
if(data.responce === "closeSelf")
{// if the user detected that there is a newer with this url opened.
window.close();
}
}
}).always(function(){ setTimeout(checkTabs, 1000);});
}
checkTabs();// to call for the first time.
希望這可以幫助。
實際上,如果使用適當的技術,這並不是一個難題。 可以使用瀏覽器SDK來控制特定的瀏覽器標簽,該SDK可以訪問標簽管理。 JavaScript或PHP本身無法訪問特定選項卡,因為這將違反安全性。
Google Chrome瀏覽器示例:
看看chrome.tabs參考。 您可以使用標簽執行許多操作,包括嘗試執行的操作。 例如,一種顯示可以控制瀏覽器選項卡的鏈接的用戶友好方法是使用瀏覽器彈出窗口 (注意,這與常規彈出窗口不同,瀏覽器彈出窗口是“內部”瀏覽器級別的表示此特定區域可能控制瀏覽器的任何部分的彈出窗口,而不僅僅是包含鏈接的當前頁面 )。 在該彈出窗口中,您可能會看到以下內容:
瀏覽器彈出窗口(HTML):
<a id="myLink" href="http://wwww.google.com">This link will control a new tab</a>
瀏覽器彈出窗口(JavaScript):
var tabNumber;
var tabCreated = false;
document.getElementById("myLink").addEventListener("click", function(){
if (!tabCreated) {
// create a new tab
chrome.tabs.create({
url: this.href
}, function(tab) {
tabNumber = tab.id; // return ID of created tab
tabCreated = true;
});
} else {
// reload the created tab
chrome.tabs.reload(tabNumber, null);
}
}, false);
規范的最大問題是,必須為每個瀏覽器分別實現這種機制,並且需要安裝插件。 如果鏈接是唯一的瀏覽器級別訪問權限,則應考慮更改其行為,以便該行為可以成為當前頁面行為的一部分。
希望這可以幫助! :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.