Can't get reference to existing window - javascript

I need to get a reference object to a window in Firefox which was not opened by the current page, but is of the same protocol, port and host. So complies with the same-origin policy.
I was using the code below to do this:
var mainWindow = window.open('', ''MAIN_WINDOW');
This was working before Firefox 52, however since the 52 update this code just opens a new blank tab, instead of referencing to the currently open window. I've looked at the fixes in the 52 release and I can't see anything which would have directly effected this.
Doing console.log(window.name) in the window I want reference to returns ' MAIN_WINDOW'. So The window name is correct.
I am then trying to access the frames array within the window reference later on in the code. Is there a way to solve this issue or an alternative I can try?

If the windows are same-origin you can use a BroadcastChannel to let separate windows or tabs talk to each other

Related

Cannot get reference to one page from another

I am working on a web app that needs to have two parts. The one is a controller and the other is a display. Something like Google Slides in presentation mode. The controller has a button to launch the display:
<script language="JavaScript">
function OpenMain()
{
var MainPage = window.open("TheUltraSignalLite.html");
TimerIMG = MainPage.document.getElementById("TimerIMG");
TimerIMG.src = "TM-Full-Blue.jpg";
}
</Script>
The call to window.open seems to return null. I have tried Chrome, Edge, Firefox, and Opera and they all have the result. These are all local files for now, but I might put in on a web server someday. I have seen some answers that want you to turn off security, but I cannot ask everyone who uses this app to turn off security. How do I get a valid reference to the display window?
Edit 1:
Yes, window.open from the local disk does cause a CORS restriction.
I tried this where both files are in the same AWS S3 Bucket, so the CORS should not be an issue. But I still get a null on the window.open. If I put a breakpoint on the first line, then everything worked. If I split the open and the rest of the code into two functions with two buttons, it works. So it looks like I have to find a way to run the open in an async way.
Edit 2
My solution to keep it simple was to put the window.open in the OnLoad event. This opens the child window and allows it to fully render and the value of MainPage is ready to use. (I changed the MainPage to a global variable.) I still have to run it from some type of web server, rather than loacl file, but that is not a big deal.
If you are not allowed to access the new window content, then the problem you are encountering is a basic security feature of web browsers. Citing mdn:
The returned reference can be used to access properties and methods of the new window as long as it complies with Same-origin policy security requirements
To read more about Same-origin policy
If your new window respects the Same-origin policy, then you can access the content of the new window with for example:
// Open index.html from the current origin
const newWindow = window.open('index.html')
const h1 = newWindow.document.querySelector('h1')
If you want to avoid asking users for pop-up permission, then you should probably use a link instead of a pop-up.

window.open why some user's IE open new window every time.

This issue is driving me crazy, I have an app which is referenced via .asp pages and in one of these pages there is a javascript function to open a popup
window.open("popup.aspx", "myPopup","width=300,height=100,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,alwaysLowered=yes,location=no,directories=no,titlebar=no");
Looking up various documentation it has been stated that if the same window name is used then this window will be re-used. This is correct on most user's IE but on an some end users PC it opens an additional popup window even though the same code is being used.
To verify this I created a test.aspx and simply duplicated the open function previously stated. On my PC I got one popup window, on the some users got two. It’s definitely Internet Explorer on this PC because I have installed Firefox and there is no issue and the same window is referenced.
IE(11) version is same all.
What problem??
Save an object reference of the opened window to a global variable and do a validation before calling window.open:
var windowObjRef = null; // global variable
if(windowObjRef== null || windowObjRef.closed)
{
windowObjRef = window.open("popup.aspx", "myPopup","width=300,height=100,status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,alwaysLowered=yes,location=no,directories=no,titlebar=no");
}
else
{
windowObjRef.focus();
}

Opera window.open without changing URL

I am using javascript method window.open to get the reference to the other windows of mine service.
The main idea is that if I use window.open('', name ) and window with given name exsists then I get the reference to it (and if it is from same domain I can comunicate with it). If it does not the new window has url 'about:blank'. So if it is about:blank I am closing it.
That works on every browser except Opera... When I am calling window.open with empty string as first argument on every browser if the window with this name exsits I will get refenrence to it and nothing more. But not on Opera - there the URL of this window will be changed to about:blank.
Are there any way to workaround this on Opera?
I'm suggesting window.focus('name'); :)?
I know this is an old question, but there are still Presto Opera users out there, and here's a solution (more of a hack) for it (yes, browser sniffing is required):
var popup = window.open("file:///D:/nonexistent_file", name);
This way you'll get the window reference and a insignificant security exception in new window (if a new window with about:blank url was opened).

window.opener is not right

I'm opening a popup and want to have a click action in it cause the execution of some jQuery in the page that opened it. Everything I find online says I should be able to do that with window.opener, (e.g. JQuery - Write to opener window)
But when I console.log window.opener, it's just 'true', not a real object. window.opener.jQuery is undefined as is window.opener.$ and 'window.opener.document'.
Here's the window open code:
window.open('http://google.com' , "moderatorWindow", 'width=300, height=300');
This is in Safari. Other pages are able to launch a popup and when I inspect window.opener on those, I get a real object. What am I doing wrong?
Your variable is true and not an object because of same-domain policy rules. Just like an iframe, if the popup you open is not on the same domain or sub-domain then it is lost to you after you create it. The web would be a very unsecure place if I could say, open a (hidden) iframe on my site to gmail.com and was able to read your email.
Even if the popup is on a sub-domain you have to do extra work and set the document.domain value of both windows to the root domain (eg. mydomain.com). This is to ensure that the popped-up site wants to be known to its parent (again, think security, if my coke.ning.com community could open a hidden iframe to your pepsi.ning.com and do brute force attempts at a login, etc.)
To prove my point try actually going to google.com and opening up Firebug (or Inspector if you're using Safari or Chrome) and doing:
var bob = window.open('http://google.com' , "moderatorWindow", 'width=300, height=300');
bob.window.location.href; // returns "http://www.google.com/"
Lastly, feel free to call jQuery on the child page to modify elements on the same page and vice-versa but you can't use jQuery from one page to modify the dom of the other page. I tried this a few years ago to try to save on some loading time and, unless something has changed, it doesn't work. jQuery seems to be bound to the window object of where it was created. Weird things happen.
Presumably you are calling:
console.log(window.opener);
which should call the toString() method of whatever window.opener references. It should reference a window object, which is a host object. Per the ECMA-262, a host object's toString() method (if it has one) can return anthing it likes, even throw an error (try it on certain IE host objects implemented using ActiveX).
This article might help: http://developer.apple.com/library/safari/#documentation/AppleApplications/Conceptual/SafariJSProgTopics/Articles/Cross-documentmessaging.html

is window.open("", ... impossible with firefox?

In firefox I have opened a locally stored file with the file:// protocol
(file:///c:/temp/foo.html)
foo.html contains Java Script which (among others) is supposed a new
window without URL:
var new_window = window.open("","", "height=100,left=50,width=200");
When this line is reached, Firefox displays this "Firefox prevented this site from opening a pop-up window". I don't understand why Firefox gives this warning, obviously, the file (foo.html) is under my control (since it's stored locally and I have opened it with the file:// protocol, and, additionally, the window to be opened doesn't point to any file that could contain any sensitive data, as the url parameter in the open method is set to "".
But besides all this, it seems I can't even force or allow firefox to open the window anyway. There's this "options" button on the yellow "Firefox prev...." bar which supposedly should allow to create exceptions, yet I can't.
So, the question basically boils down to: how can I allow a local html file to open an empty window with Javascript within Firefox.
Thanks / Rene
This is a Firefox security precaution, see this link:
http://kb.mozillazine.org/Links_to_local_pages_don't_work
However, it looks like this extension will allow you to override it:
https://addons.mozilla.org/en-US/firefox/addon/281
This is the popup blocker, which block popups not opened by an explicit user action like a click.
You cannot force it to open the popup, you need to allow Firefox to open it.
I suggest you to test the new_window variable to see if it is null. In this case, display a message to the user so that he allows the domain to open popup windows.

Categories