I am using Mozilla Firefox and I am trying to figure out a way to access the content of other tabs in the same window using JavaScript and the DOM (I am open to other techniques if exist).
E.g., I want to run JavaScript code in tab1 which can find the title of some other tab. Basically I need this so that I can identify a tab which has opened due an href in my current page without using window.open method. All I want is a simple hyperlink which opens a page belonging to the same domain as the current page (the page should be opened in a new tab). Now I want to be able to access this new tab from the current tab.
Whilst you can easily open a new window using JavaScript, I'm sure that is as far as it goes. From a security point of view you wouldn't want JavaScript in one tab being able to query / access the DOM in another tab. Any site would then be able to gain access to your bank account details, etc. if both sites were opened in separate tabs.
You can access the new window/tab if it was opened with JavaScript and the page indeed is in the same domain.
You can open the window/tab like so
var win = window.open("/path_to_page");
Then you'll have to wait for the page to load before you can access e.g. the title.
win.onload = function(){ alert(win.document.title); };
You could use HTML5 cross-window messaging (archive.org link...but that's kind of cutting edge.
Even in that case, you'd probably need to hijack the <a> tag 'click' event with JavaScript and open the window yourself so that you'd have access to the new window object for posting messages.
Try setting a cookie which is accessible to any page in the same domain. On other pages, use a JavaScript timer to check if the cookie value has changed and when it has you can use its value and take an action.
It worked for me.
Well, this would not be possible, you could try
<a target="_blank" rel="opener" href="about:blank"></a>
This makes a link that opens an about:blank, this will have the same domain as the page that opened It because of the Same-Origen-policy.
Related
I am using Mozilla Firefox and I am trying to figure out a way to access the content of other tabs in the same window using JavaScript and the DOM (I am open to other techniques if exist).
E.g., I want to run JavaScript code in tab1 which can find the title of some other tab. Basically I need this so that I can identify a tab which has opened due an href in my current page without using window.open method. All I want is a simple hyperlink which opens a page belonging to the same domain as the current page (the page should be opened in a new tab). Now I want to be able to access this new tab from the current tab.
Whilst you can easily open a new window using JavaScript, I'm sure that is as far as it goes. From a security point of view you wouldn't want JavaScript in one tab being able to query / access the DOM in another tab. Any site would then be able to gain access to your bank account details, etc. if both sites were opened in separate tabs.
You can access the new window/tab if it was opened with JavaScript and the page indeed is in the same domain.
You can open the window/tab like so
var win = window.open("/path_to_page");
Then you'll have to wait for the page to load before you can access e.g. the title.
win.onload = function(){ alert(win.document.title); };
You could use HTML5 cross-window messaging (archive.org link...but that's kind of cutting edge.
Even in that case, you'd probably need to hijack the <a> tag 'click' event with JavaScript and open the window yourself so that you'd have access to the new window object for posting messages.
Try setting a cookie which is accessible to any page in the same domain. On other pages, use a JavaScript timer to check if the cookie value has changed and when it has you can use its value and take an action.
It worked for me.
Well, this would not be possible, you could try
<a target="_blank" rel="opener" href="about:blank"></a>
This makes a link that opens an about:blank, this will have the same domain as the page that opened It because of the Same-Origen-policy.
I have a JavaScript application in a tab which opens if I click on a certain link on my page. If I click on the link again, I need to check if that tab is allready opened - if it is, then switch to that tab and do something, otherwise open a new tab.
Unfortunately using
var myApp = window.open("http://www.mypage.com/myapp.html", "My App");
if(myApp){
won't help me, because I can't rely on the URL, due to the fact that the URL will differ on each environment that I'm using.
How can I achieve this if I can't rely on the URL? The only thing I can check is the tab's name - it will allways be the same. How can I access the browser's tab array, please?
Thanks!
As far as I understand, you cannot access the browser's tab array (certainly not through JavaScript). This would be a security breach on the client side. You should think of each browser tab as a browser instance of it's own - unaware of any other tab.
You can track that if tab is opened already using the Cookie/Local storage.
Save the value "isOpened=true" in either Cookie or localStorage and Be sure to delete the values from the same when that tab is Closed or when user navigates to other pages using "onunload" event.
But the page you are going to open should be on the same Domain for accessing/ setting the Cookie.
Still we cannot do anything if the user opens the page by just copying the link.
as you are opening new window using javascript you can do this you can add a dummy attribute say "data-isOpend='no'" on that element
and when user click on that element you can change that attribute to "data-isOpened='yes'"and in javascript function before opening tha new window you have to check if data-isOpend=='no' then open link in new window else do nothing
(Just leaving a differenct ans here if any other dev comes here looking for solution for such prob,Just like me :) )
We can assign name to the tab while creating it
For eg.
function view_preview()
{
var abc;
if(abc!=null){
abc.close();
}
var url=some_link
abc=window.open(url,'tab_name');
}
Here, we are opening a new tab and if that tab is already opened we are 'refreshing' it.
Hard to come up with a title, my apologizes.
Problem is this: Since modern web-browsers disable pop-up windows I am in need of a work-around.
When a visitor comes to the website they are prompt to press a button. Once the button is pressed a pop-up window is launched with the following code:
w = window.open('/audio/audioplayer.php?id='+audioId, 'audioplayer', params);
Now that the pop-up is open I would like when the visitor views other pages the pop-up is loaded with specific information based on whatever page they are on.
I am not sure if this is possible or how I can do this (check if the pop-up window is open, and if it is load the information, and if its not re-display the button)
I don't think it is possible to detect where the popup is open of not.
Have you thought about using a dialog? Rather than a popup?
window.open returns a windowObjectReference - this is the only way you can talk to the popup window. In particular, you can tell if that window is closed with the windowObjectReference.closed attribute. And the popup window has a window.opener attribute that references the parent window back. You can use both to communicate.
However, it seems you want to keep this communication between page loads. You have a few options:
Try to keep the link between windows as long as possible. The problem is that when the parent window reloads, all the javascript variables reset and there's no way to recover the reference to the popup - unless the popup sets it using window.opener. This link shows this approach and also another one with frames.You could consider it either ugly or clever. But it's not perfect. (You can't do anything if the user opens a page in a new link)
Communicate with the server using ajax from both main pages and the popup page. When a top level page wants to send a message to the popup, they start an XMLHttpRequest to your server which notifies a script which leaves a message in a "queue". The popup page regularly polls/long-polls the server with XHR too (or server sent events, my personal favorite) and updates its own contents accordingly.This might be a bit more complex/expensive than you'd like but it's also the safest solution.
Don't use popups, like the other answer suggested. A div with position: fixed could get you a similar result, and might save you from that method of communication between windows, however it also leads to having one dialog per page, so you need to ask the server if another instance of the dialog is running. Not quite sure if other methods of sync are viable for this (localstorage?)
I am creating a site which makes use of youtube videos, but I don't want to use the embedded player, I want to just have a youtube page in an IFRAME. I read the youtube TOS and I am not sure whether IFRAMES are allowed other than the embedded player so taking no chances I think I will build the site to open links in new tabs instead. What I would really like is to be able to open new links in just one tab. To clarify, my site is tab 1, once a link is clicked youtube opens in tab 2, any further links clicked on my site replace the content in tab 2, and no more than 2 tabs will exist.
YouTube and some other high-profile websites block the use of iFrame for security reasons. So if you used an iFrame, it would just load blank.
To open a link to a new tab you would use:
Open me!
I'm not entirely sure that you can control which tab to then replace with further content.
My suggestion would be to do this:
1) Create links to pages with videos embedded in them, with target="_blank".
2) When a user clicks the link, capture it with Javascript (perhaps jQuery) and open the contents of that within some kind of modal overlay like Shadowbox ( http://www.shadowbox-js.com/ ).
This way, the user stays within the same window, without the need for iFrames or new tabs at all. If the user has JS disabled, they'll then get a default "new tab" behavior.
Good luck.
I don't think you control that what you want very easily, and if you can find a hook for this, it most likely is not cross-browser. I'm open to a better suggestion but I think you could use javascript window.open(URL, windowName[, windowFeatures]) and use the same name every time you call it. Then it will replace the previous window. It just will not open in another tab but in a popup.
You cannot control a webpage in another tab and neither can you limit the no. of tabs a browser can open for obvious reasons of security. Limiting the no. of tabs that can be opened or controlling without user's intervention would definitely be considered hacking, bowsers don't allow that.
This is the situation. I'm trying to provide a service where someone embedds an iframe on their website with a form in it. At the end when an ajax request comes in again I want to pop a new window with a thank you note in it. window.open does not work and my guess is because the window object belongs to the page that embedds it and not the iframe and would then be considered cross-site scripting. Is there another way of doing this?
A thought I had was, that I can create links with target="_new" in my iframe, and clicking that would actually pop another window. Maybe I could create this link and "click"/trigger it with javascript?
I do have control over what the user embedds so potentially I could include a script there too, but the less code there, the better obviously.
Any takes?
window.open does not work and my guess is because the window object belongs to the page that embeds it
I don't think so. window as seen by your script will generally be the window object of the document inside the iframe.
More typically, a window.open on an XMLHttpRequest completion will be blocked by browsers' built-in pop-up-blockers. They usually only allow new windows to be opened in direct response to user interaction (typically, in the onclick event handler).
Maybe I could create this link and "click"/trigger it with javascript?
No, otherwise everyone would be doing that to circumvent blockers!
If you're starting an XHR and you know you're going to need a pop-up in the future you'll have to open it now and put some filler content in until the XHR returns, at which point you can update its contents.