Click on a link to open it in a new window/tab WITHOUT leaving the current page

C

chambers.anya

Hi,
I've just been scouring the web trying to find an answer for how to do
this but so far without any success.

Basically I want some of the links in my page to open in new tabs (I'm
using Firefox) or windows if the user has their options configured
that way. And I want my page to remain intact as it involves some AJAX
and takes a while to load.

But ...and this seems to be a big "but"... I don't want the focus to
move away from the page I'm on. So techniques such as <a
target="_blank"> or <a onclick="window.open(...)"> aren't working for
this requirement. I even tried using a separate function and
attempting to return to the original window straight after using the
following function:

// Open a URL in a new window or tab (depending on browser-specific
settings) and move focus to it
function openLinkInNewWindow(link) {
var newWindow = window.open(link);
window.opener.focus();
}

But in Firefox this is just causing both tabs to open the new link.

So now I'm scratching my head and wondering if it's going to be
possible at all. Is there something I've missed? Thanks a lot for any
help or advice (but please don't condone me for opening these links in
new windows or ask why I want to do it, trust me, I do!!!)

Cheers
Steve
 
M

matth

Hi,
I've just been scouring the web trying to find an answer for how to do
this but so far without any success.

Basically I want some of the links in my page to open in new tabs (I'm
using Firefox) or windows if the user has their options configured
that way. And I want my page to remain intact as it involves some AJAX
and takes a while to load.

But ...and this seems to be a big "but"... I don't want the focus to
move away from the page I'm on. So techniques such as <a
target="_blank"> or <a onclick="window.open(...)"> aren't working for
this requirement. I even tried using a separate function and
attempting to return to the original window straight after using the
following function:

// Open a URL in a new window or tab (depending on browser-specific
settings) and move focus to it
function openLinkInNewWindow(link) {
var newWindow = window.open(link);
window.opener.focus();

}

But in Firefox this is just causing both tabs to open the new link.

So now I'm scratching my head and wondering if it's going to be
possible at all. Is there something I've missed? Thanks a lot for any
help or advice (but please don't condone me for opening these links in
new windows or ask why I want to do it, trust me, I do!!!)

Cheers
Steve

Try FF's openNewTabWith(). If that doesn't work look under the cover
and see if you can get the functionality you're looking for.
 
C

chambers.anya

Try FF's openNewTabWith(). If that doesn't work look under the cover
and see if you can get the functionality you're looking for.

Thanks for your reply matth but Firefox-specific code isn't really an
option here as I need this to work in all (recent) browsers.
 
T

Thomas 'PointedEars' Lahn

Basically I want some of the links in my page to open in new tabs (I'm
using Firefox) or windows if the user has their options configured
that way. And I want my page to remain intact as it involves some AJAX
and takes a while to load.

But ...and this seems to be a big "but"... I don't want the focus to
move away from the page I'm on.

Tough luck. Focus windows is under the control of the user, too. Firefox
has a preference for this (dom.disable_window_flip), and a UI for that:
Tools, Options, Content, Raise or lower windows. The default is `true'
(the checkbox is unchecked), meaning that Firefox does not allow that.
[...]
// Open a URL in a new window or tab (depending on browser-specific
settings) and move focus to it

Either the description above or this description is wrong. From the above,
I take it that you _don't_ want to focus the new window or tab. Assuming
this is what you actually want to achieve:
function openLinkInNewWindow(link) {
var newWindow = window.open(link);
window.opener.focus();

`window' does not refer to the last opened window/tab (that is what
`newWindow' refers to here), but to the *current* window/tab. You are
looking for

window.focus();

instead. The usual feature tests should be applied.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Tough luck. Focus windows is under the control of the user, too. Firefox
has a preference for this (dom.disable_window_flip), and a UI for that:
Tools, Options, Content, Raise or lower windows.

Tools, Options, Content, [x] Enable JavaScript, "Advanced...", (Scripts will
be allowed to do the following:) Raise or lower windows.
 
T

Thomas 'PointedEars' Lahn

matth said:
Try FF's openNewTabWith(). [...]

| >>> openNewTabWith("http://example.net/")
| [Error:] openNewTabWith is not defined
|
| >>> window.openNewTabWith("example.net/")
| [Error:] window.openNewTabWith is not a function
|
| >>> navigator.userAgent
| "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7
| Gecko/20070914 Firefox/2.0.0.7"

JFYI: This method is available to native Gecko code and (Firefox)
add-ons, not to unprivileged JavaScript. And that is good so.

http://developer.mozilla.org/en/docs/DOM:window.open#FAQ


PointedEars
 
C

chambers.anya

Hi PointedEars,
First thanks for the replies but unfortunately it seems I'm no nearer
to where I need to be...see below.
Cheers, Steve

[...]
// Open a URL in a new window or tab (depending on browser-specific
settings) and move focus to it

Either the description above or this description is wrong. From the above,
I take it that you _don't_ want to focus the new window or tab. Assuming
this is what you actually want to achieve:

Yes you're right, the description was wrong. This was left over from
previous functionality where the new window did get the focus but as
you rightly guessed I now want to keep the focus on the original page.
`window' does not refer to the last opened window/tab (that is what
`newWindow' refers to here), but to the *current* window/tab. You are
looking for

window does seem to refer to the tab that has just been opened since
the focus switches to it immediately. However, as mentioned, using
this method I am finding that the new page loads in both tabs (i.e. in
the "spawned" tab and the "opener" tab.) Even if I add an extra line
newWindow.focus() to guarantee that the focus on the new window, this
makes no difference in practice and the same thing happens.

window.focus();

instead. The usual feature tests should be applied.

As mentioned, window refers to the new tab at this point so this
doesn't work - I've tested it and find that the line has no effect.
 
H

Henry

On Sep 25, 8:23 am, Thomas 'PointedEars' Lahn wrote:


window does seem to refer to the tab that has just been opened
since the focus switches to it immediately.

Seeming and being are not the same thing.
However, as mentioned, using this method I am finding that
the new page loads in both tabs (i.e. in the "spawned" tab
and the "opener" tab.)

The error that is likely to be generated when you attempt to execute -
window.opener.focus() - on a page that has no - opener - may explain
that symptom.
Even if I add an extra line newWindow.focus() to guarantee
that the focus on the new window, this makes no difference
in practice and the same thing happens.

Did you add that before or after the line that is generating an error?
As mentioned, window refers to the new tab at this point

No it does not.
so this doesn't work - I've tested it and find that the line
has no effect.

Do you mean that line has no effect at all (and if window/tab focusing
by scripts is being restricted in the browser it can be expected to
have no effect anyway) or that is has no effect alongside the error
producing line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top