为提升您的使用体验,本站正在维护,部分功能暂时无法使用。如果本站文章无法解决您的问题,您想要向社区提问的话,请到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 提问,我们的支持社区将会很快回复您的疑问。

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

I want to open external links in already opened tab (web 2.0 apps hash links)

  • 5 个回答
  • 1 人有此问题
  • 10 次查看
  • 最后回复者为 Jose Canciani

more options

I have a web 2.0 app, where the app is intelligence enough to detect hash changes (# or #!), and update appropriately.

An email comes to my inbox, with a link to the app (ex: myapp.com/#Case/2013). I already have an open tab in this domain, let's say: myapp.com/#Dashboard.

So I want Firefox to be clever enough to reuse the tab and just update the url, and the app will do the rest.

Is this possible out of the box, do I need an extension? It seems more and more the web is filling with these type of apps, and opening links in new windows/tabs all the time is useless, since webapps usually has their own tab management or similar way of navigating its content.

Thanks! Jose

I have a web 2.0 app, where the app is intelligence enough to detect hash changes (# or #!), and update appropriately. An email comes to my inbox, with a link to the app (ex: myapp.com/#Case/2013). I already have an open tab in this domain, let's say: myapp.com/#Dashboard. So I want Firefox to be clever enough to reuse the tab and just update the url, and the app will do the rest. Is this possible out of the box, do I need an extension? It seems more and more the web is filling with these type of apps, and opening links in new windows/tabs all the time is useless, since webapps usually has their own tab management or similar way of navigating its content. Thanks! Jose

被采纳的解决方案

After searching a bit more, I decided to build my own extension, and after a couple of hours digging stack overflow and the sdk doc, I built what I needed. Here's the code:

var tabs = require("sdk/tabs"); var pageMod = require("sdk/page-mod"); pageMod.PageMod({

   contentScriptWhen: 'start', // This says not to wait until the page is ready
   include: ['*.yourdomain.com'],
   onAttach: function(worker) {
       var thisTabUrl = worker.tab.url.split('#');
       var isAlreadyOpened = null;
       for (var tab in tabs) {
           if (tabs[tab].id != tabs.activeTab.id) {
               var thatTabUrl = tabs[tab].url.split('#', 1);
               if (thatTabUrl[0] === thisTabUrl[0]) {
                   worker.tab.close();
                   tabs[tab].activate();
                   tabs[tab].url = thisTabUrl.join('#');
                   return;
               }
           }
       }
   }

});

Regards, Jose Canciani

定位到答案原位置 👍 0

所有回复 (5)

more options

This is in a stand-alone mail client? Firefox's built-in options for external links are:

(1) First tab in a new window

(2) New tab in the latest window active in Firefox (default)

(3) Replace contents of last active tab in Firefox

While (3) is close to what you want, it's not intelligent enough to match up with the actual tab you would want to update.

(If you are clicking a link in webmail, your options are similar, but the default is to replace the current page unless the link is coded to target a new window, in which case the default is to open a new tab in the current window.)

I suspect you will need an extension.

One way that applications re-use their own window or tab is to re-use a specified "target". While typically links are launched in a new tab using the special target name "_blank", if the link uses a target name that Firefox already has opened (such as "myappwindow") then in theory the link should target that window. In order for this to work, though, the existing page you want to replace needs to have been opened either from a link with that target or the JavaScript window.open() method with that target. To my knowledge, there's no way to assign that target name retroactively.

more options

It occurs to me that if you are implementing this for customers and they have unsaved work in the tab you're replacing, you may need to implement some kind of autosave to avoid angry support calls.

more options

Thanks for the answer. I mentioned email client just to try to say "any other program opening a link in the default browser firefox" :) In my case, it was Thunderbird.

I guess I will need an extension, I'll leave the problem open some time to see if someone knows about one that do this. I looked but did not find one.

Does the use of target in a link reload the tab content? Or does it just change the url when we are using just a different hash? This could indeed solve at least partially the problem when opening links from inside firefox, although we wil need to modify our app to use target. Anyway, it seems mail clients strip the target part of the anchor tag, so it doesn't help much :(

Thanks, Jose

more options

Hi aerotux, when you target a named tab, the new URL loads in place of the current content. I haven't tested what happens when the only change is the hash...

more options

选择的解决方案

After searching a bit more, I decided to build my own extension, and after a couple of hours digging stack overflow and the sdk doc, I built what I needed. Here's the code:

var tabs = require("sdk/tabs"); var pageMod = require("sdk/page-mod"); pageMod.PageMod({

   contentScriptWhen: 'start', // This says not to wait until the page is ready
   include: ['*.yourdomain.com'],
   onAttach: function(worker) {
       var thisTabUrl = worker.tab.url.split('#');
       var isAlreadyOpened = null;
       for (var tab in tabs) {
           if (tabs[tab].id != tabs.activeTab.id) {
               var thatTabUrl = tabs[tab].url.split('#', 1);
               if (thatTabUrl[0] === thisTabUrl[0]) {
                   worker.tab.close();
                   tabs[tab].activate();
                   tabs[tab].url = thisTabUrl.join('#');
                   return;
               }
           }
       }
   }

});

Regards, Jose Canciani