[英]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.