SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question find out when page is opened in new-window or new-tab ?

    Hello,
    I was wondering if there was a way once could check if a page was opened in a new tab, by right-clicking and clicking the "Open in New Tab" option in most browsers, or if a page was opened in a new window, by right clicking on the link and pressing the "Open in New Window" in a browser?

    Is there a way one could do this in js? or, and i know this is not the forum to ask, but if possible in html or php?
    I am asking here because js seems like it would be the most-capable of doing this. Also if it could be done in jQuery, that would be convenient for me

    Finally, the reason why I want to do this is to detect if a link from / in my site was opened in a way besides the default action i put in the html <a> tag's target attribute.

    Thank you for you help and please post any Questions, Comments, Concerns, or Solutions.

    Regards,
    Team 1504

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The second parameter of window.open() provides the name of the window or tab to open it in. If you use '_blank' then it will always open in a new window unless your visitor has overridden that option. If you specify a value that doesn't start with an underscore and a window or tab (or frame) already exists with that name then the page will be loaded in that existing window or tab. The fourth parameter can be set to true or false to indicate how the existing history associated with that window or tab should be treated.

    Anyway, telling your visitor to open the context menu and select which of the three places to open the page is a far better solution to trying to force it to open in a particular spot using JavaScript. Anything you place in the window.open(0 call apart from the first parameter are just suggestions and the browser can be configured to ignore them and always open the page in a certain way.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well I dont want to dictate how they open the link. I want to know how they opened the link.

    And if they opened the link in a new-tab or new-window then execute some js

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by team1504 View Post
    Well I dont want to dictate how they open the link. I want to know how they opened the link.
    And if they opened the link in a new-tab or new-window then execute some js
    Sorry no, web browsers do not give that that sort of information to the scripting environment.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    oh okay. well it was worth a shot.
    I'll just have to exhibit less control on my site's environoment then...
    haha thank you!

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,226
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Wouldn't testing for
    window.history.length
    work OK?

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Wouldn't testing for
    window.history.length
    work OK?
    Yes, that should work in enough environments. If it's equal to one you can assume that they're in a new window or tab. You just won't be able to determine from that which of those two it is.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Yes, that should work in enough environments. If it's equal to one you can assume that they're in a new window or tab. You just won't be able to determine from that which of those two it is.
    First of all, Thank you both for still posting even though I had given up.

    Hmm, well for the purposes of the site i was making, I don't need to distinguish between an open tab or open window just know that its one of them.

    So how would one test for window.history.length to do this?

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by team1504 View Post
    So how would one test for window.history.length to do this?
    You would compare window.history.length to a number
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well I thought so. Sorry if that sounded rude, I didn't mean it to be. I should have made it clear that I figured one would have to do that. A question that I am clueless on is that number would you compare the length of the history to?

    Also does window.history.length give the length of pages in the history of the specific window /holidays tab it is run in?
    If so then would I compare the value to 0. Meaning the page was either the users home-page or opened in a new-window or new-tab, right?

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by team1504 View Post
    A question that I am clueless on is that number would you compare the length of the history to?
    You would compare the length of the history to the number you would expect on a new page.

    Quote Originally Posted by team1504 View Post
    Also does window.history.length give the length of pages in the history of the specific window /holidays tab it is run in?
    Yes.

    Quote Originally Posted by team1504 View Post
    If so then would I compare the value to 0. Meaning the page was either the users home-page or opened in a new-window or new-tab, right?
    No, because the history also contains the current page that you are on, so you would compare it with 1.

    You can get some further details about it at Manipulating the browser history
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah okay. 1 not 0 because they are at a page, which makes it in the history. I will give it a try tomorrow.

    Thank you all for your help!

    Beat Regards,
    Team 1504


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •