Using window.open repeatedly produces unexpected result - javascript

My browser allows any website to show pop-ups (made using window.open).
window.open("http://www.google.com/","_blank");
The above code opens google.com on a new tab.
However, when I use window.open repeatedly like this:
window.open("http://www.google.com/","_blank");
window.open("http://www.facebook.com/","_blank");
window.open("http://www.example.com/","_blank");
Now, google.com is opened on a new tab, while facebook.com and example.com are opened as pop-up windows. I actually want to have the 3 links to open in new tabs but I can't achieve that.
How can I fix this?

I'm surprised the second and third get opened at all. (They don't on Chrome, it blocks them; but I do see the behavior you describe if I disable the blocker.)
You'll need to provide separate UI elements (buttons, whatever) to open each window individually, so that each window.open call is triggered by a separate end-user action, rather than opening several windows in response to a single user action. Hopefully that will give you the behavior you desire for each individual window. Although you don't have control over whether something opens in a new window or a tab, most browsers default to new tabs these days (and most have an option for users to change it) unless you supply the third argument to window.open, in which case they may open a new window instead using the settings you pass in that argument.
Since Chrome does this unusual thing with the second and third calls, limiting yourself to a single open per user action should work in most cases, as in most cases the default is a new tab, not a new window.

Related

Keep focus on specific tab in Edge

in a last stint to change from EI to Edge, I'm trying to do some focusing that doesn't work like I want it to.
The situation is something like this. Edge is open with several tabs for different pages. One of these pages is an intranet system where the pages contains a link which opens a popup.
In this popup the user has a button where they can create some docx files on a server. To do this, the button calls a new page (subpage1) through javascript (window.open) and the new page again calls another page (subpage2), which finally creates the document.
The later (subpage2) will close (windows.close) it's window on completion.
It all works like it should, with the little annoyance, that the "mother" windows in Edge will switch to the tab to the far right, instead of just staying on the user choosen tab.
Can I control which tab keeps or regains focus when a tab are closed in Edge and if so how?

Can i open / focus a different tab within the same domain that i didn't open myself?

I have two instances of my Angular App running in two different tabs, I would like to be able to let the user switch between them by pressing a button on the site.
I can communicate between the instances using the Broadcast Channel API, however calling window.focus() inside the inactive tab doesn't do anything. Since the messages are serialized / deserialized I also can't just send the window object over the channel.
I also tried getting access to the window instance of the inactive tab by setting window.name in it and then calling window.open('', otherName) in the active tab. That also doesn't work and just spawns a new empty tab.
A solution would have to work for existing instances of the app opened by the user, I know this would work if i just window.open'ed the second instance from the first one (Or iframed it or something similiar). I also know this would work using a browser extension but that also doesn't fit my use case.
If this simply isn't possible I will just build "tabs" into the application itself but just thought i would ask first since this way would be easier.

How to recall browser tab with previous instance on same page

I have a page with a few links.
Each link opens in a new tab.
The next time I click a given link, I want it to recall the same tab where it was previously opened.
Is it possible? How?
All pages come from the same domain.
I've seen this answer:
Check if my website is open in another tab
It seems possible to store the URIs in localStorage, for repetitions detection.
I think this technique will allow me to detect repetitions, but I can't figure out how to aim at the specific tab (activate it, bring it to the front).
I don't control the server, need to implement it in the client (via Tampermonkey).
For the curious, I work a lot with BitBucket issues.
The page with the links is the issues listing, the other pages are the individual issues.
I want to be protected from opening the same issue twice, update both versions, and have a data loss.
Additionally, each page can have two different URIs, one for viewing and the other for editing. But I think I can solve it with JS.
Recalling a Browser Tab by Name
You can first specify a name for each tab you create, by using window.open():
let newWindow = window.open(newUrl, newWindowName);
or by using target attribute in an anchor tag:
Make New Window
If you then call window.open with the name of an existing tab, that will use the existing tab. If the window by that name doesn't exist, window.open will create a new window (or tab).
MDN web docs page for Window.open
Bringing Window to the Front
Using window.open() alone may not be enough to bring the window to the front. That may be possible with a different function, window.focus(), which issues a request to bring the window to the front. Combining the two:
window.open(myUrl, myWindowName).focus();
Caution: A browser's user preference setting may still prevent focus() from bringing the window to the front, so this is not guaranteed to work. From MDN web docs:
It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.
You can get the previous page's URL with the following:
let prevURL = document.referrer;
You can get the URLs from all open tabs with the windows.getAll() function.
From this answer - You cannot programmatically focus the browser to a specific tab due to security concerns. Consider updating the title to notify the user with document.title = {{new title}}

IE8 a href for local files opens new window despite target="_Self"

My set of files are all on a local server, not http. If I use:
click here
it opens the file in a new window. _Top is also ignored, and I get a new window. I can get around it by using window.open() but for various reasons we need to avoid javascript.
I'm interested to know why the a href target is ignored.
You want to use _self (or just leave it off altogether): http://jsfiddle.net/rsmRA/ -- this is a case-sensitive value.
According to the spec, it doesn't seem like it's supposed to be case-sensitive, so I guess this is a browser bug. If you set it to anything except those keywords it is a "browsing-context name." You can see this in action by clicking that same link twice; you will notice that it only opens a new window the first time and reloads that window the second time.

link running a javascript that open another page. how to make it do it in new tab if user uses middle click or right click menu

I have this link in my left navigation:
dashboard
That javascript opens a link based on the passed parameters.
All works fine, but I would like to be able to use the browser capabilities of opening the links in a tab (when user is using middle click or selects 'Open link in new tag' from right click menu). Though, this is not working for links handled with javascript code.
There are many reasons why this is not the default behaviour of the browser (e.g. javascript function might only do some validation and stay in the page ... browser can't know what the js might do or if a new window/dialog will result from that action so would make no sense to open new tag as a result of a middle click ...). But hopefully there is a workaround for the default behaviour.
Any idea how this could be done?
Cheers,
Stef.
Javascript links execute in context of the page where they are called. If you "open" the link in a new tab/window, the javascript code will be executed in the new window, i.e., empty, and will most probably fail.
A browser could try to add the feature you are asking for by cloning the page which contains the link, and executing the javascript code in the context of the cloned page. But this would most likely break some critical sites (imagine for example that your online banking site works with javascript, so when you open a link in a new tab/window, cloning the original window might lead to a duplicate transaction).

Categories

Resources