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.
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.
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.
oh okay. well it was worth a shot.
I'll just have to exhibit less control on my site's environoment then...:nono::mad::confused::eek:
haha thank you!
Wouldn't testing for
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?
You would compare window.history.length to a number
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?
You would compare the length of the history to the number you would expect on a new page.
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
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!
This topic is now closed. New replies are no longer allowed.