Open links in new tab in Firefox - javascript

I am developing an Firefox extension. How can all the links on a webpage to be opened in a new tab?

That's usually a configurable option in Firefox to handle new links, so they may override your extension with that.
However...
The code
Example Website
will allow you to click the appearing words [Example Website], and the link will open in the current window.
The code
Example Website
Opens the link in a new window/tab.
The only mildly dodgy thing is that target is now apparently deprecated by the W3C, which means that it's generally up to the browser ( and the user's preferences) as to how (or even if) it is handled. But for people who have their preferences set accordingly - in Firefox - that should work.

I found what I was after. I wanted gbrowser.addtab(this.href).

Press Ctrl while clicking on the link on Windows. Use cmd on OSX.

Related

how return focus on parent window using javascript?

is it possible return focus on tab parent using javascript?
I read some threads about this problem, but i didn't find solutions.
I tried
window.opener.parent.focus()
and
window.opener.focus();
but it doesn't work.
Can someone help me?
Thanks
Generally, you cannot do this inside a web page. Because it's the user's choice which tab/window she wants to focus on and browsers such as firefox and chrome respect such choices by providing configs to open new tabs in the background or not. But under several very special cases, you may still achieve this.
If you want to open a new tab and return focus immediately, you can try to simulate a 'ctrl+click' event on a link to open the tab on the background. Refer to this thread Open a new tab in the background?(Only for chrome, API may already changed. So it may only works on an obsoleted version)
If you are shipping with an extension, do it in the extension code. For example: in chrome extension.
If your script is for a customized browsers which you have control on / you can affect the design, you can implement the function in the browser side and expose an API for your script.

Use window.open to a tab in the background (not switch focus)

I'm using the following code to open a new tab on click of a PDF download.
The problem is the new tab becomes the main tab often before the PDF loads.
How can I make the view stay on the current window (PDF) and open the new tab but not switch to it?
Note: In Chrome and Opera they understand the HTML5 download tag so the PDF simply downloads and the current window redirects - All good! So this is only a problem on IE & Firefox.
<h2 style="text-align: center;"><a href="http://cdn2.hubspot.net/hub/155045/file-847580737-pdf/Stepping_into_a_new_age_of_marketing_with_CRM_FINAL_APPROVED.pdf" onclick="casestudiesopen()" download><strong>Click here to download your eBook</strong></a></h2>
<script>
function casestudiesopen() {
window.open("http://www.workbooks.com/case-studies");
}
</script>
Well, I'll advise you to read this Stackoverflow answer, which is, in a way, quite similar to yours (the purpose anyway) :
Javascript disable switches current tab functionality in browser
JS/JQuery is indeed very powerful but also have its limits. Imagine a web page always requesting and keeping focus once you've opened it. I think you would be really annoyed, among other things.
That's why browsers prevent those kind of actions. Common browsers at least. Meaning, there's no way to prevent a browser like Firefox, Chrome, IE & Co. to focus a table since it depend of user's parameters.
You'll have to find a way to workaround your problem. I can propose this answer since it seems to have worked for the other guy.

Firefox addon: Window.open() vs CTRL-N

I created a simple firefox add on using addon-builder that installs & successfully appears in the add on toolbar at the bottom of the browser.
If I press ctrl-n, open new tab, open new window, or open private browsing window in firefox, then I see and can use my addon. However, if another site programmatically opens a window using window.open(), then my addon doesn't appear.
Is this by design? Or is there a setting that I can include in my addon so that it always appears, even if the window was opened through window.open() instead of ctrl-n?
When sites open with window.open, they specify which parts of the browser UI will show. You may be able to place it somewhere less likely to be removed, like the navigation toolbar. The add-ons toolbar sounds like it's going away soon, anyway.

Javascript to open URL within a new Tab (instead of a window)

Hey guys. There are a few entries here requiring solutions to do the opposite of this, and others vaguely related. In one of them, the poster asked how to do this on Mozilla Firefox. Actually though, firefox will always open the URL in a new tab when window.open() is called, unless you set the window's size within its parameters.
So Mozilla and Chrome do what I want by default. The question is: how do I get Internet Explorer to open the URL I want within a new tab, as opposed to doing it in a new window?
Thanks in advance.
The obvious answer is to open the link with target="_blank". As you said, Firefox and Chrome will open a new tab.
Regarding IE - the behavior is up to browser preferences. By default (in IE7+ obviously) I believe the behavior is defined as open new tab. If the user decided the behavior should be a new window, there's only so much you can do about it.

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