I'm in a discussion with my co-worker. We have a web application in a tabbed browsing environment, say with 2 tabs open. Tab2 has a link to tab1, when I click the link, it opens tab1 in a new tab. We want to detect if tab 1 is already open, if so then set focus to tab1 (and not open a new window).
He has gotten this working in a broswer window environment, but says it is not possible in a tabbed environment.
Is it possible to for javascript to determine if tab1 is already open and then just set focus to that tab?
It is only possible if the webbrowser is already preconfigured to open links with a target in a new tab instead of new window and sets focus to it itself when you have given the links the same target name.
E.g.
link1
link2
link3
In other words: you're dependent on the client/webbrowser used. You have no control over it from the server side on.
In Firefox for example, the client should have checked both the Open new windows in new tab instead and When I open a link in a new tab, switch to it immediately options to get your requirement to work.
(as you see, the last one is disabled in mine)
You should instead create a separate document with a DOM-based tab, and iframes pointing to the real pages. Then you could easily set 'focus' to the correct tab when needed.
Related
I have an application which opens some larger multi-page dialogues in new browser tabs using window.open(). On the first page in the new tab close() works, but after any navigation in the new tab close() doesn't work any more in modern browsers, all workarounds I found have been disabled by now.
Is there any way to keep the reference to the initially opened window and pass it along during navigation so I can close it from within the new tab after the navigations?
Or access the tab which opened the current tab initially which still holds a reference to the opened window and close it from there?
All pages are controlled by me and share the same host / context if this may help, navigation in the new tab must be top level to some extent, so wrapping into an iframe may not be an option.
This is within the context of an edge extension but it will work with Standard JavaScript.
The extension must;
1 Be able to recognise when a new tab is opened.
2 Replace the new tab with a designated URL rather than about:blank etc.
3 Have links that appear in popups open within the active tab, not a new one.
I have this JavaScript;
function handleCreated(tab) {
var newTabURL = {url: ''};
browser.tabs.update(newTabURL);
}
browser.tabs.onCreated.addListener(handleCreated);
as an example. It does replace the current tab with google but only when a link in the popup is clicked. The link from the popup should be what replaces the active tab OR if the user creates a new tab it should be replaced with mycompany.com/newtab.html, for example.
I've got an unpackaged example with everything so you can see all of the code. To use this you must first enable developer options in MS Edge (Here's how), then you can load the extension.
The extension in its current state has code notes and explains within the UI. You can grab the extension in its current form from this link. You'll just need to download and unzip then load folder within edge. I figured this is easier than me posting all of the code as you can better see how elements interact with each other.
So in summary;
1 The New Tab should open with the custom URL https://mycompany.com/newtab.html.
2 Typing Google should redirect to https://gooogle.co.uk. (Adding word shortcuts with JS in the address bar)
3 Links opened from the popup should replace the active tab that the popup is available over and not open in new tabs.
It works a little bit, I'm just struggling to get the new tab function to work. This isn't intended for the MS store so the policies aren't relevant.
Download the zipped extension folder here.
MS Edge opens links from a popup in a new tab by default, and this tab isn't active.
If for example you had;
<p>Visit Google</p>
the URL would open in a new tab. To open the URL in the tab that is active within the Edge browser you can instead do the following, so it is actually possible.
1 Place the element in a DIV, for example linked content and use <div id="linkedcontent> within your HTML.
2 Place the content that should trigger the new tab within the DIV.
3 Use the following JavaScript to getElementByID and then use .onclick to replace the current browser window.
document.getElementById("linkedcontent").onclick = function() {
var LinkedContent = {url: 'https://google.com'};
browser.tabs.update(LinkedContent);
};
This will then result in the browser window being replaced by the URL specified. In my example it's https://google.com. If you need to do this multiple times just duplicate the code and change the DIV id to something unique for each and reflect this in the JavaScript too.
My use case:
User already has web app open in a browser tab
An email is sent to the user with an anchor tag that takes user to a specific page in the web app
On clicking, the initial tab is repopulated with new page
To do this the anchor tag is formatted like <a target="webapp" href="example.com" >Click Here</a> and I have a tiny bit of javascript, triggered on load, in my web app like this window.name="webapp"
What is working:
If I test to make sure the window name was with window.name="webapp", doing console.log(window.name), it confirms that it was
If I click the link from the email and the web app isn't open yet a new tab opens. If I click that link again the tab that initially opened refreshes.
What isn't work:
If the user initially navigates directly to the web app (not via a link) and then clicks the link from the email, the link opens a new tab instead of repopulating the open tab. In this scenario I still get the correct window name in the console log but not the functionality I'm looking for.
Looking for insight into how to get this to work or if there's a better solution to this use case.
In the website I am building I have a link to another webpage set with a target of _blank to open in a new tab.
If I click the link and then switch back to the 1st page and click the link again it opens a 3rd tab.
Is there any way to get the browser to just switch back to the already open tab for the 2nd webpage?
You can use target="secondPage", this create a named window/tab and every links that a user click that has this target will be opened in this page.
Other than this, I don't think it's possible nor a good thing. You should not tell the user how to manage his tabs/windows.
I have same ancors/hyperlink in my html file. These point to a new website outside my site.
So I want that when the user clicks the link it should open a new tab or window. My website page should not be closed.
How can it be done?
open in tabs: there is nothing programatically that you can do to accomplish that, the only thing I'm thinking of is set the browser to open new links in tabs instead of new window...
to open in a new window all you need is to place a target in your anchors
click here
and, by the way there are more options to the target
target="_blank"
opens in a new Blank window
target="_parent"
opens in a Parent window (used when dealing with iframes and you want to open the link in other frame)
target="_self"
opens in it's own/self window (used when dealing with iframes and you want to open the link in the current frame)
target="_top"
opens in the top of all frames (it will open on top of all frames in the page, like a no-frame page will be display)
Click me to open in a new tab/window
Load the linked document into a new
blank window. This window is not
named.
If there is no existing window or
frame with the same name as specified
in the target, a new window is opened
with a name equal to the value of the
target.
Its the setting in a browser that determines whether to open the page in a new tab or in a new window.
Just add target="_blank" to the <a> tag
Click here
The easiest way is to use the target attribute in your anchors and set it to _blank:
Worse Than Failure
This is a dupe of opening links in the same window or in a new (tab) and https://stackoverflow.com/questions/118567/is-there-ever-a-good-reason-to-force-opening-a-new-browser-window. Basically - no. And there shouldn't be a way - you shouldn't impose your surfing preferences on unsuspecting others.