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

Discussion in 'Javascript' started by chambers.anya@googlemail.com, Sep 24, 2007.

  1. Guest

    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
    , Sep 24, 2007
    #1
    1. Advertising

  2. matth Guest

    On Sep 24, 5:48 pm, 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.
    matth, Sep 25, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 25, 2:32 am, matth <> wrote:
    > On Sep 24, 5:48 pm, 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.
    , Sep 25, 2007
    #3
  4. Re: Click on a link to open it in a new window/tab WITHOUT leavingthe current page

    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, Sep 25, 2007
    #4
  5. Re: Click on a link to open it in a new window/tab WITHOUT leavingthe current page

    Thomas 'PointedEars' Lahn wrote:
    > 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, Sep 25, 2007
    #5
  6. 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/DOM:window.open#FAQ


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Sep 25, 2007
    #6
  7. Guest

    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 <>
    wrote:
    > 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.
    , Sep 25, 2007
    #7
  8. Henry Guest

    On Sep 25, 1:21 pm, 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.
    Henry, Sep 25, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    2
    Views:
    1,227
  2. Jeremy Brown
    Replies:
    9
    Views:
    598
    Stewart Gordon
    May 10, 2006
  3. Blue®
    Replies:
    4
    Views:
    775
    Blue®
    Sep 27, 2003
  4. JoJo
    Replies:
    1
    Views:
    139
  5. Replies:
    1
    Views:
    225
Loading...

Share This Page