Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Click on a link to open it in a new window/tab WITHOUT leaving the current page (http://www.velocityreviews.com/forums/t932961-click-on-a-link-to-open-it-in-a-new-window-tab-without-leaving-the-current-page.html)

chambers.anya@googlemail.com 09-24-2007 09:48 PM

Click on a link to open it in a new window/tab WITHOUT leaving the current page
 
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


matth 09-25-2007 01:32 AM

Re: Click on a link to open it in a new window/tab WITHOUT leaving the current page
 
On Sep 24, 5:48 pm, chambers.a...@googlemail.com wrote:
> 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.


chambers.anya@googlemail.com 09-25-2007 06:39 AM

Re: Click on a link to open it in a new window/tab WITHOUT leaving the current page
 
On Sep 25, 2:32 am, matth <matt...@gmail.com> wrote:
> On Sep 24, 5:48 pm, chambers.a...@googlemail.com wrote:
>
>
>
> > 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.


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.


Thomas 'PointedEars' Lahn 09-25-2007 07:23 AM

Re: Click on a link to open it in a new window/tab WITHOUT leavingthe current page
 
chambers.anya@googlemail.com wrote:
> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Thomas 'PointedEars' Lahn 09-25-2007 07:27 AM

Re: Click on a link to open it in a new window/tab WITHOUT leavingthe current page
 
Thomas 'PointedEars' Lahn wrote:
> chambers.anya@googlemail.com wrote:
>> 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.


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

> The default is `true' (the checkbox is unchecked), meaning that Firefox
> does not allow that.


Thomas 'PointedEars' Lahn 09-25-2007 07:41 AM

Re: Click on a link to open it in a new window/tab WITHOUT leavingthe current page
 
matth wrote:
> 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...indow.open#FAQ


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

chambers.anya@googlemail.com 09-25-2007 12:21 PM

Re: Click on a link to open it in a new window/tab WITHOUT leaving the current page
 
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

On Sep 25, 8:23 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> chambers.a...@googlemail.com wrote:
> > [...]
> > // 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.

> > 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 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.


Henry 09-25-2007 12:32 PM

Re: Click on a link to open it in a new window/tab WITHOUT leaving the current page
 
On Sep 25, 1:21 pm, chambers.a...@googlemail.com wrote:
> On Sep 25, 8:23 am, Thomas 'PointedEars' Lahn wrote:

<snip>
>>> 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 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?

>> window.focus();

>
>> instead. The usual feature tests should be applied.

>
> 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.



All times are GMT. The time now is 09:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.