简体   繁体   English

在浏览器中重新加载标签内容

[英]Reload tab content in browser

how can I reload specific browser tab content? 如何重新加载特定的浏览器标签内容? I search for solution which can not be only in JQuery (I prefer it if there more solution). 我搜索不能仅在JQuery中使用的解决方案(如果有更多解决方案,我会选择它)。

I need this: 我需要这个:
I have page with a lot of links on another's pages (format of links: http://domain_name.cz/?still_same_text=1_1_1_1_1_1386662409 ; another page has this link: http://domain_name.cz/?still_same_text=1_1_14_1_2_1387231396 etc. everyone links has same format only to 1st number). 我在另一个页面上有很多链接的页面(链接格式: 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等。每个人链接的格式仅与第一个数字相同)。 When I click first time on some link then I load new tab (for this text I say him simply "tab2") if I click again on another link then I reload this "tab2" (= I change url in this "tab2" from current to new => I load new conent in this tab). 当我第一次单击某个链接时,我会加载新标签页(对于此文本,我说他只是“ tab2”),如果我再次单击另一个链接,则会重新加载此“ tab2”(=我从以下位置更改此“ tab2”中的网址)当前为新=>我在此标签中加载新的内容)。
Is there some way to do that? 有什么办法吗?

I try solution from this topic but this not work for me and, I mean, I have difficult problem. 我尝试使用主题的解决方案, 但这对我不起作用,我的意思是我遇到了难题。 And I look at solution in this topic, but this is another problem. 我正在研究主题中的解决方案,但这是另一个问题。 There is 1 link but I can have more links and there are links in JS but I need generate them in PHP. 有1个链接,但是我可以有更多链接,并且在JS中有链接,但是我需要在PHP中生成它们。

I've not done this before, but the question seems interesting. 我以前没有做过,但是这个问题似乎很有趣。 Therefore, if the answers you mentioned did not work then I would try the following concept, it may not be the best, but it's something. 因此,如果您提到的答案不起作用,那么我将尝试以下概念,它可能不是最好的,但这是有的。

1- Make a tracker on the server (properly in session) to keep track of the pages that the user has opened. 1-在服务器上(正确地在会话中)创建一个跟踪器,以跟踪用户打开的页面。 So whenever the user open a page, make an ajax request sending the page url with a random generated id number to be stored and checked later. 因此,每当用户打开页面时,都会发出ajax请求,以发送带有随机生成的ID号的页面网址,以供以后存储和检查。 Each tab will get a different id. 每个标签将获得不同的ID。

2- Add a javascript listener in all pages that keep waiting for commands from the server. 2-在所有等待服务器发出命令的页面中添加javascript侦听器。 Depending on the server response, those listen will do the following: 根据服务器的响应,这些侦听将执行以下操作:

  • Nothing -> in case the page is opened first time. 没什么->第一次打开页面的情况。

  • Close tab -> in case a new tab with same link is opened. 关闭选项卡 ->如果打开了具有相同链接的新选项卡。 So keep the new tab and close the old one. 因此,保留新标签并关闭旧标签。 // Or close new one and refresh old one? //还是关闭新的并刷新旧的?

  • Update -> update server once tab is opened (directly when the page gets loaded) and just before they get closed , send the page url and current tab id to be check on the server. Update- >更新服务器,一旦打开选项卡(直接在页面加载时),并且在关闭之前 ,请发送页面URL和当前选项卡ID以便在服务器上进行检查。

  • Keep checking server, if a newer tab with same url is opened, then close current. 继续检查服务器,如果打开了具有相同URL的较新选项卡,则关闭当前窗口。

I'm not sure if this solution is suitable for you, so I will only write a pseudo code. 我不确定该解决方案是否适合您,所以我只会编写一个伪代码。 If it's applicable, then later maybe can improve it a bit. 如果适用,那么以后可能会有所改善。

pseudo code php: tabControl.php 伪代码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"))
}

And from the client side, you can use something similar to this pseudo code: 在客户端,您可以使用类似于以下伪代码的内容:

//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. 

Hope this helps. 希望这可以帮助。

In fact, this isn't a difficult problem if you use appropriate technology. 实际上,如果使用适当的技术,这并不是一个难题。 Controlling specific browser tabs can be done with the browser SDK, which has access to the tab management. 可以使用浏览器SDK来控制特定的浏览器标签,该SDK可以访问标签管理。 JavaScript or PHP themselves cannot access a specific tab because it would be a security breach. JavaScript或PHP本身无法访问特定选项卡,因为这将违反安全性。

Example for Google Chrome: Google Chrome浏览器示例:

Take a look at the chrome.tabs reference. 看看chrome.tabs参考。 There is many things you can do with tabs including what you are trying to do. 您可以使用标签执行许多操作,包括尝试执行的操作。 For example, a user-friendly way of displaying a link that can control browser tabs would be to use a browser popup (attention, this isn't the same thing as a conventional popup, a browser popup is an "internal" browser-level popup that is suggesting that this specific area may control any part of the browser instead of the current page only ) that contains the links. 例如,一种显示可以控制浏览器选项卡的链接的用户友好方法是使用浏览器弹出窗口 (注意,这与常规弹出窗口不同,浏览器弹出窗口是“内部”浏览器级别的表示此特定区域可能控制浏览器的任何部分的弹出窗口,而不仅仅是包含链接的当前页面 )。 In that popup, you could have something like this: 在该弹出窗口中,您可能会看到以下内容:

Browser popup (HTML): 浏览器弹出窗口(HTML):

<a id="myLink" href="http://wwww.google.com">This link will control a new tab</a>

Browser popup (JavaScript): 浏览器弹出窗口(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);

The biggest problem of your specification is that this kind of mechanism must be implemented for each browser separately and requires a plugin installation. 规范的最大问题是,必须为每个浏览器分别实现这种机制,并且需要安装插件。 If the link is the only browser-level access, you should consider changing its behavior so he can be part of the current page behavior. 如果链接是唯一的浏览器级别访问权限,则应考虑更改其行为,以便该行为可以成为当前页面行为的一部分。

Hope this helps! 希望这可以帮助! :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM