Javascript 在浏览器中重新加载选项卡内容
如何重新加载特定的浏览器选项卡内容?我搜索的解决方案不能只在JQuery中(如果有更多的解决方案,我更喜欢它) 我需要这个:Javascript 在浏览器中重新加载选项卡内容,javascript,jquery,html,browser,Javascript,Jquery,Html,Browser,如何重新加载特定的浏览器选项卡内容?我搜索的解决方案不能只在JQuery中(如果有更多的解决方案,我更喜欢它) 我需要这个: 我有一个在其他页面上有很多链接的页面(链接的格式:;另一个页面有这个链接:等等。每个人的链接都有相同的格式,只有第一个数字)。当我第一次点击某个链接时,我会加载新标签(对于这个文本,我简单地说他是“tab2”),如果我再次点击另一个链接,我会加载这个“tab2”(=我将这个“tab2”中的url从当前改为新=>我会加载这个标签中的新内容)。 有什么办法吗 我试着从这个话题
我有一个在其他页面上有很多链接的页面(链接的格式:;另一个页面有这个链接:等等。每个人的链接都有相同的格式,只有第一个数字)。当我第一次点击某个链接时,我会加载新标签(对于这个文本,我简单地说他是“tab2”),如果我再次点击另一个链接,我会加载这个“tab2”(=我将这个“tab2”中的url从当前改为新=>我会加载这个标签中的新内容)。
有什么办法吗
我试着从这个话题上解决问题,但这对我来说不起作用,我的意思是,我有一个难题。我看了一下主题中的解决方案,但这是另一个问题。有1个链接,但我可以有更多的链接,JS中也有链接,但我需要用PHP生成它们。我以前没有这样做过,但这个问题似乎很有趣。因此,如果你提到的答案不起作用,那么我会尝试以下概念,它可能不是最好的,但它是有意义的 1-在服务器上制作一个跟踪器(正确地在会话中),以跟踪用户打开的页面。因此,每当用户打开一个页面时,都会发出一个ajax请求,发送带有随机生成的id号的页面url,以便稍后存储和检查每个选项卡将获得不同的id。 2-在所有等待服务器命令的页面中添加javascript侦听器。根据服务器响应,侦听者将执行以下操作:
- 无->如果页面是第一次打开的
- ->如果打开具有相同链接的新选项卡。因此,保留新选项卡并关闭旧选项卡还是关闭新的并刷新旧的
- ->打开选项卡后(直接在加载页面时)更新服务器,并发送页面url和当前选项卡id以在服务器上进行检查
- 继续检查服务器,如果打开了具有相同url的较新选项卡,则关闭当前选项卡
//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本身无法访问特定选项卡,因为这将违反安全性 谷歌浏览器示例: 看看参考资料。你可以用标签做很多事情,包括你想做什么。例如,显示可控制浏览器选项卡的链接的用户友好方式是使用(注意,这与传统弹出窗口不同,浏览器弹出窗口是“内部”浏览器级弹出窗口,表明此特定区域可以控制浏览器的任何部分,而不是仅控制当前页面)包含链接的。在该弹出窗口中,您可以有如下内容: 浏览器弹出窗口(HTML): 规范中最大的问题是,这种机制必须分别为每个浏览器实现,并且需要安装插件。如果链接是唯一的浏览器级访问,则应该考虑改变其行为,这样他就可以成为当前页面行为的一部分。
希望这有帮助!:-) 我认为重新加载标签内容是浏览器插件的一项工作,比如Chrome扩展编码,但我不知道通过命名目标名称是可能的。祝你好运。你无法通过控制客户端选项卡。在你的webapp中使用javascript。正如Ken OKABE提到的,浏览器级扩展可能是唯一的出路。感谢您的评论和这些信息。您介意使用服务器来完成这项工作吗?我的意思是,这不是问题。但当我看到这个解决方案时,我可以检查这个解决方案是否正确(我不知道我可以/不能对服务器做什么)。
<a id="myLink" href="http://wwww.google.com">This link will control a new tab</a>
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);