Open new tab in background in Firefox with Javascript? - javascript

I want to use JS to open a new tab in Firefox but in the page group. That will be Google, in the example. I want the original tab to stay focused and open Facebook. Would also work in as many other browsers as possible, ideally.
I am using this code, and it works, in a way.
The first click will open the new tab and focus on that, which is not what I need.
But, if I make the first tab, the original, go back then press it again, I get the desired behaviour. The new tab will open in the background but the original one will be the one that's focused and changes to the webpage.
<button type="button" onclick="open_in_bg('http://facebook.com', 'http://google.com')" >Press Me</button>
<script type="text/javascript">
function open_in_bg(c_url, n_url)
{
window.open (n_url, "mywindow" );
window.open (c_url+"#maintain_focus","_self");
}
</script>
My first thought is that it may be possible to replicate the result from the 2nd click for the 1st. Possible? Any viable solution?

Sorry to say, I do not believe this is possible. Most browsers have removed any support for controlling tabs via javascript as a security issue. This was removed to prevent pop-unders by nefarious sites.
Here is an info about the user settings

Related

Opening two urls with one click while avoiding popup blocker in Chrome

My objective is to open two URLs in two tabs with just one click with a single link.
So far I have tried two javascript codes (found in this forum that attemps to open 3 URLs) and a jquery code.
It seems that Internet Explorer and Mozilla in their default settings allow this to happen. While in Chrome, I am getting the pop-up blocker. I know that it would be just a matter of allowing the pop-up to happen in chrome in order for the multiple links to open, but it would be a huge advantage for the experience I am trying to build, to have all browsers providing same user experience: one click, two URLs opened in two tabs.
Is there any workaround as of now that you might know that avoids getting the pop-up blocker in Chrome?
Below there are two the javascript codes and the jquery one that I have been testing. All of them have the same effect in chrome triggering, and succeed in Internet Explorer and Mozilla.
I thank you in advance.
JAVASCRIPT
<a href="#" target="_blank" onclick="window.open('http://google.com');
window.open('http://yahoo.com');" >Click to open Google and Yahoo</a>
<a href="http://bloggersentral.blogspot.com/" target="_blank"
onclick="window.open("http://www.tourism.gov.my/");
window.open("http://www.tic.kelantan.gov.my/");">This anchor opens three links in a single click</a>
JQUERY
<body>
Click
<script>
$('.each').click(function() {
var urlList = "http://www.google.com,http://www.yahoo.com"
$.each( urlList.split( "," ), function( index, item ) {
window.open( item, "_blank" )
});
})
</script>
</body>
The user has to manually allow the popups because otherwise it would mean that there could be a whole lot of spam websites. So no.
But also here is a very simple html code for it
Click Here
You could ask the user to disable it: How do I detect popup blocker in Chrome?
I think the only way to prevent pop up blocker blocking popups is that the user says "allow popups for this page". Otherwise this would be a security issue.
If there was an easy workaround to the popup blocker than it wouldn't do a very good job at blocking popups, would it? The best solution in this scenario would either be to separate the links into two separate clicks, or inform the user that they may have to manually allow the tabs to be opened if they have a popup blocker enabled. So in short. No, you can't but there are plenty of better ways to approach the issue.

Open multiple pages in once with javascript/ jquery

I want to create elements which (when clicked) will guide visitors towards two differenct websites (both in new windows). How can I do this? Below are the two ways which I tried, which both do not work as only one page/ window is opened. Thanks in advance!
Timo
The solution as suggested here: How to make a link open multiple pages when clicked . But maybe the solution is outdated, but for me only the first webpage is openend (with all the indicated solutions. (for example the code which is suggested there)
HTML Click Here
$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('https://www.youtube.com/watch?v=H9XIXFwpyEc');
window.open('https://stackoverflow.com/search?q=open+multiple+pages');
});
While both using a class and an id with both a different a href and both with the extra options of opening new window also does not work as it only opens the first window
HTML Click Here
$('.yourlink').html(''); // Add page
$('.yourlink a').attr('target', '_blank');// Make sure it opens in new window
$('#extra').html(''); // Add page
$('#extra a').attr('target', '_blank');// Make sure it opens in new window
Using Chrome, I'm not seeing the problem you describe. But when I tested it in IE, I suddenly got a "popup blocked" message.
It's not strange though. I'd hate it if a single click on a link could suddenly spawn 10 new windows. In this scenario I actually think IE handles it better (by blocking the second window).
The thing is that window.open will only work if the action that invokes it is a trusted event. That usually means a user-initiated event, like click. But what Chrome doesn't account for (I assume) is that a single trusted event can then invoke several window.open.
I've tried to work around this feature, but have not (yet) been able to fool IE. The options, as I see it, are:
Ask users to add your site to the popup exceptions (internet settings).
Spawn the pages in iframes within your own site
Ask users to use another browser ;)
Or the obvious:
Use separate links for the windows

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.

Creating new tab / switching between Tabs in Firefox?

I am looking for a way to improve the workflow in a PHP based CMS. There is a lot of switching between the editor mode and the preview mode of the page. The editor mode is huge to load, and so I would like to open the preview mode in a different window.
I don't want to use new windows or an iframe within the current window to keep the workflow simple and to avoid confusion.
Is there a way to explicitly open a new tab (not window), and to jump to that tab from a document, in Firefox? The number of users is limited, so there is the possibility to set up the client with the necessary extensions / permissions.
I know Firefox can be forced to open all links in tabs, but I think that won't cut it, as I still can't address and focus the newly opened window.
Thanks for all the great answers everyone. I have now enough material to decide whether I'll take the greasemonkey approach, rely on the user to set up "open in tabs" and address the window by name, or use a "inline" HTML solution as so many of you suggested. I am accepting the answer that I feel went most effort into.
There is no way to force a window to open as a tab. It's all dependent on the user's preference settings.
I second the answers that say you should do this in HTML using Javascript. Then it can work in all browsers that support JS.
I would put two divs on the page and show/hide each div depending on which tab is selected. If you are clever about this you could trap the click on the tab and determine if the user left-clicked or middle-clicked. If they left click you load that tab on the page. If they middle-click you let the browser open a new tab/window (according to the user's prefs, don't try to force it), and leave the current window unchanged (that is, don't switch to the new tab). The action for clicking on the tab would be to use AJAX to load the contents of the remote document and put it into the tab. Use Javascript to modify the URL before submitting the AJAX request so that the server knows to send a web page fragment instead of the whole page.
The advantage of this dual-natured solution is that the tabbed approach will work the way you want it to work for the majority of cases, but for users with, say, two screens, or who prefer switching between browser tabs, they will still have the flexibility to work in multi-window mode. This can all be done without any browser extensions and it should work equally well in IE as well as Firefox, Opera, etc. Avoid locking yourself into one browser, even one as excellent as Firefox. One day a customer will need to use Opera or Safari and you'll be stuck.
You say you don't want to use an iframe to avoid confusion. Now I don't know about the layout of your website, but I've been using the approach that the editor opens in its own div right next to the content being edited and the content is being live updated as you edit. No need to change tabs.
(If the window is too narrow there are HTML tabs Edit and Preview)
It does not seem to add confusion to the user and for me this approach works really well. Maybe it's worth considering in your case.
What about using iframes and JavaScript?
I know you said you want to avoid 'confusion using iframes', but in my opinion if you really need to load different pages at the same time this is the best option.
In theory, you could create your own tab system using javascript or even better, using jQuery, because its UI module offers pretty cool tab control.
For every tab you could load separate "headerless-footerless" version of your specific admin page inside <iframe> element. If user wanted to modify something different, he will simply click on the tab and bring different iframe.
All this could also be done using AJAX, but iframe solution is quite easy as you just need to load ready page and all postbacks are already handled by original page and separated from master-admin-page.
You might also need to play a little bit to set correct height of your iframe to fit all the content without scrollbars, but this again, is just bit of javascript.
Nope, there's no way to force the opening of a new tab, simply because this would be unsupported by un-tabbed browsing
You can only set it to open a new window, not a new tab.
Greasemonkey springs to mind - a quick google gives open in tabs on left click. I think you could modify that so it only runs on one particular page, and you'd be up up and away.
This question made me wonder if HTML 5 allows that sort of specification, and it doesn't (nothing in one of the other hyperlink attributes, either). A new browsing context is a new browsing context, there's no way to express a preference for tab over window or foreground over background.
You can't force a tab, but if you use a target with a specific name, like target="my_cms_window", many browsers will open this as a new tab. Additionally, they will remember the name and if you use the target repeatedly, put the contents in the same tab. I have found that this works pretty well in the real world.

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