Open new Firefox tab with JavaScript with expected behavior - javascript

When a user clicks a link in my application, I need to "enrich" the clicked URL with some additional context (depending on the interaction) and then proceed as normal.
"As normal" means to set the window location to the new URL, or to open a new tab if a meta key is held while clicking.
This works as expected in all modern browsers except Firefox; in Firefox, the tab is always focused, regardless of what's set in Preferences > Tabs > "When I open a link in a new tab, switch to it immediately".
I can't just let the MouseEvent through, because I need to direct to a different URL than that on the clicked <a href>; I have to stop propagation of the click event.
In short, how can I get window.open('url', '_blank') to respect the user's preferences and any meta keys in Firefox?
EDIT: My question differs from Open a new tab in the background? in that my need is for Firefox to work like other browsers; Chrome is working fine here. Also, I don't want the target page to always open in the background, I want it to open in another tab on a ctrl+click/cmd+click/middle-mouse click, or in the current window on a non-meta click, and to respect user prefs for opening focused or in the background. Updated the title to address this.

Related

iOS Safari - pages opened to new tab inherit history, is this behavior documented?

Repro steps:
Open a page #1 on iOS Safari
Open a link from it into a new or background tab (#2)
Do (via safari dev tools) history.length - it will show a history of tab #1. Also, history.go(-1) will go to a page #1 in a tab #2
This is probably happening because iOS is trying to keep a smooth experience for the user (if a page opens a link into a new tab, you can still navigate back via UI button, and this will close the new tab and return to the previous tab).
I want to ask:
Is this behavior (specific to mobile safari) documented somewhere?
Is it possible to distinguish a link opened into a new tab (in other browsers, such a link has history.length == 1, but not here)? It would be helpful if I want to help user get back, as I'd need to do window.close() (= close tab #2) instead of history.go(-1) (= opens page #1 in tab #2).
Thanks!

Parent window freezes when window.open which triggers print onLoad

I have a scenario where in my webpage i do a window.open() which opens up another window whose onLoad() invokes window.print(). In this flow, the parent window remains frozen disallowing any links to be clicked on. How to I get away with this problem?
For example:
An HTML page has three links: create, manage and print. The javascript code for the print link is the following:
var url = '/actions/print/';
var win = window.open(url,"Title_","resizable=yes,scrollbars=yes,directories=no,titlebar=no,location=no,status=no,menubar=no,width="+width+",height="+height);
now, the HTML rendered by /actions/print is:
<html><body onload="window.print();"> ... Content goes here ...</body></html>
So as you can see, clicking on print opens up a new window and triggers print immediately. In this state, when i go back to parent window with three links and try to click on other links, it doesn't work and appears frozen.
You can't resolve this issue on OS X. Which I am pretty sure you only tested on.
Here is how I came to that conclusion after testing on different browsers and plateforms.
Chrome, Safari and Firefox on OS X did not allow interactivity with parent window when a print modal was opened from a child window (closing the print dialog, but not the pop up, re-enabled interactivity).
IE8 on Win XP, IE11, Chrome and Firefox (all on Win7) always allowed interactivity with this same scenario.
This led me to believe this was an OS X restriction or recommendation. On further research, here is what I found on developer.apple.com, confirming my doubts:
A document-modal dialog prevents the user from doing anything else within a particular document. The user can switch to other documents in the app and to other apps. Document-modal dialogs should be sheets, which are described in Using Document-Modal Dialogs (Sheets).
Use a sheet when multiple documents can appear in a single window at different times. For example, a tabbed browser can display different documents in a single window at different times. A sheet is appropriate in this situation, even though it applies to only the document that is currently visible in the window. Because users must in effect dismiss the current document before viewing a different document in the same window, they should first dismiss the sheet.
https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/OSXHIGuidelines/WindowDialogs.html

Control the tab in which an externally called URL is opened

I've got a program (not a web application, but an executable) that spews URLs, the URLs are handled by Internet Explorer by default.
The problem: Each URL is opened in a new tab whereas it should be the same tab.
If it's relevant: I'm trying to match an interface of an old (web)application to a new one. The URL is redirected on the server. In the old situation, the URL would open a webpage, start an Active-X control and then close the browser. I'm not in such position now, I cannot use that technique because the new application is a full fledged zero footprint webapp.
Remember that I can't control the browser. It's not opened by me so I cannot do fancy stuff in Javascript for instance.
How to handle this? The best thing I can come up with is changing settings in Internet Explorer, so that it'll always throw away what is in the current tab and replace it with the new URL. Is this possible at all?
I'm not even interested in the tabs as such, if I can always open URLs in an running browser in for instance the first tab I'll be a happy camper.
Thank heaven for co-workers.
In Internet Explorer do Alt+T, O to start the internet options dialog.
In the dialog click the button 'Tabs' and the dialog 'Tabbed browser settings' appears.
Here in 'Open links from External programs in:' select the option 'The current tab or window'.
It does have a drawback that whatever browser or tab is active loses it's content when an external program fires a URL, but that's less offensive than opening browser tab after browser tab...

How to get Chrome to open multiple sites in a new tab

I'm developing a tool that lets you open multiple pages at once with a shortcut, to be used for things like opening your daily sites or querying multiple search engines for a phrase. In Firefox, Internet Explorer and Opera, assuming you've unblocked pop-ups for the domain, the code works as expected.
Chrome, however, opens the sites in new windows instead of tabs if the links are opened automatically when the page loads. If openAll() is commented out and the button is clicked or a key is pressed, the pages open in tabs. Note it's calling the exact same function.
The best solution I've found (which isn't saying much) is the One Window extension. It works, but you can see the new window open then get sucked back in, and it keeps you from opening new Windows with Ctrl-N, forcing you to drag tabs out to use another Chrome window.
I can understand there not being a programmatic way to change this because it's a browser setting, but as a user of the tool it's annoying to have the sites all open in new windows. Is there a Chrome setting or extension that will open links in tabs when they're loaded without user input? I realize opening a bevy of windows is the very thing browsers aim to stop, but this is one time where I want to allow it.
<input id="openAllBtn" type="button" value="Open all links"> (Or press any key)
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
function openAll() {
window.open('http://yahoo.com/');
window.location.replace('http://www.bing.com/');
return false;
}
$(document).ready(function() {
$(document).bind('keypress', openAll);
$("#openAllBtn").bind("click", openAll);
openAll();
});
</script>
Here's a Fiddle of the code: http://jsfiddle.net/sfzjR/
Is there a Chrome setting or extension
that will open links in tabs when
they're loaded without user input?
Check out the create method in the chrome extension docs. By default it will open a new tab, you can optionally specify the window you want that tab to open in, and give the tab a url.

Firefox javascript bookmarklet open tab in background

I've written a bookmarklet to look a word up in a Chinese dictionary:
javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){window.open('http://nciku.com/search/all/'+Qr);})();
This opens a new tab with search results for your selected word or a word you type in at the prompt. Is there a way to load the new tab in the background? I'd like to keep the focus on the page I'm looking at, and look at the search results later.
There is an option "When I open a link in a new tab, switch to it immediately" in Firefox, this doesn't help.
Edit: Note that this is for my use, so an answer that tells me how to change Firefox (3.0.11) settings to do this would work as well. Also I've tried the following modification, but it's still focusing the new tab.
javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('%E8%AF%8D%E8%AF%AD',''))};if(Qr)(function(){var%20oldWin=this;window.open('http://nciku.com/search/all/'+Qr);oldWin.focus()})();
Edit 2:
Looking around to see if I can find an answer I see this guy who's got the opposite problem (new tabs don't get focus, but he wants them to have it), but with no resolution:
Possible to set tab focus in IE7 from JavaScript
There's apparently talk about a _tab target in HTML 5, but that doesn't help me much.
http:/ /forums.whatwg.org/viewtopic.php?t=185&highlight=tab+focus
(apparently as a new user I can only post one link, so I've mauled it)
This seems pretty broken browser behaviour if this is impossible.
In FireFox type about:config and change browser.tabs.loadDivertedInBackground to true.
This has worked for me with browser bookmarklets.
source: http://lifehacker.com/263940/force-links-to-open-in-the-background
No, not programmatically through JavaScript. You don't have control over the user's browser preferences, only they have control over that.
Moreover, even if you did have control over that, you shouldn't do it, because it undermines the control that your script is given to you by the browser. If the user wants a page to open in the background, they should be able to control it, not you, as the developer.
Apparently this is only possible with previously opened windows, not the root window.
Calls to window.open with the same
window name as an already existing
window, loads the URL into that window
and gives a reference to the window
back. The window isn't given focus,
its opener property isn't changed, and
a third argument to window.open is
ignored. You can use the focus method
to give the window focus manually.
var oldWin = window.open("url.html","oldName");
oldWin.focus(); // give focus
Facing the same issue, I only noticed that if you alert() something just after opening the window, Firefox would not switch to the newly opening tab.

Categories

Resources