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

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();
}

Related

Can't get reference to existing window

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

Is it possible for a window to retain a window.opener reference without having been opened by window.open()?

I'm wondering if it is possible in the newer versions of JavaScript (I'm using React ES6, if that factors in at all) for a script in a window to have a window.opener where window.opener !== null, without having first been opened by the same script using the open() command? That's a confusing question so let me try to clarify.
I am currently experiencing a console error in my JS application (run in Chrome) that reads: Scripts may not close windows that were not opened by script. I've seen many questions on SO detailing workarounds for using window.close() on a window that wasn't opened by the current script, but that's not quite my question. My code reads:
if (window.opener) {
window.close();
} else {
// do something else
}
window.close() is, as far as I know, the only function which can generate this browser error Scripts may not close windows that were not opened by script. But I also would have thought that if there were a non-null reference to window.opener, the same script I'm running must have been the opener of the current window. So, how is window.close() erroring out, saying that this script didn't open the window, but there's a window.opener reference which is non-null?

HTML transient modal window

We have a legacy web application. At various places it opens a window with the help of Privilege Manager on Firefox to get the needed result.
Some of these windows open a Java applet or a PDF document.
The client machines are updating Firefox and Privilege Manager is gone.
What is the easiest way around it?
The problems are :
There must be only one instance of the pop-up at anyone time. This could be done by selecting appropriate window name on window.open() call.
If the window is opened again (by means of user action), it should not reload but just focus to bring it to the foreground (I have seen I can keep a reference to the window on JavaScript to do that)
It basically really must be transient/modal so that the client cannot leave the current page or reload or any other kind of interaction with the parent window (except opening/refocusing the child window) without closing the child window first. I have no idea how to do that.
Do anyone has an idea how to do that?
The client is only Firefox (it works in a special kiosk configuration) on Linux.
I read somewhere that I could somehow write an extension but I am basically clueless about extensions and its API.
Edit1:
Example of (simplified) legacy code. Not really sure if all the permissions were required, but this is it: This function opens a window that stays over the parent window and prevents any interaction from the user with the parent window.
function fWindowOpen(url, name) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
netscape.security.PrivilegeManager
.enablePrivilege("CapabilityPreferencesAccess");
netscape.security.PrivilegeManager
.enablePrivilege("UniversalPreferencesWrite");
netscape.security.PrivilegeManager
.enablePrivilege("UniversalPreferencesRead");
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
window.open(
url,
name,
"screenX=70,dependent=yes,menubar=0,toolbar=0,width=900,height=700,modal=1,dialog=1"
);
}
function fnCapture(){
fWindowOpen("/path/to/document_or_japplet/page","_blank");
}
HTML:
<button value="Capture" property="btnCapture" onclick="javascript:fnCapture();"/>
Edit2: Solution
On a typical extension, on the xul code, define this javascript code:
var dialogExt = {
listener: function(evt) {
// Do work with parameters read through evt.target.getAttribute("attribute_name")
window.openDialog(evt.target.getAttribute("url"), evt.target.getAttribute("name"), evt.target.getAttribute("features"));
}
}
// from examples
document.addEventListener("dialogExtEvent", function(e){ dialogExt.listener(e); }, false, true);
Then, on the web page:
var element = document.createElement("dialogExtElement");
element.setAttribute("url", url);
element.setAttribute("name", name);
element.setAttribute("features", features);
document.documentElement.appendChild(element);
var evt = document.createEvent("Events");
evt.initEvent("dialogExtEvent", true, false);
element.dispatchEvent(evt);
Now, maybe I am missing some security checks to let the code work if it originates from the same host, and how to handle a reference to the document that requested the dialog as means of interaction between the dialog window and it's opener.
The Privilege Manager was deprecated in Firefox 12 and removed in Firefox 17 (briefly restored).
You might want to look into Window.showModalDialog(). However, it is deprecated and is expected to go away within the year, or in 2016 if you go with an extended service release (ESR) of Firefox 38. It may be a temporary solution while you develop an extension.
In order to accomplish the same tasks, you will need to write an extension and ask the user to install it (from Bypassing Security Restrictions and Signing Code, the old information about Privilege Manager):
Sites that require additional permissions should now ask Firefox users to install an extension, which can interact with non-privileged pages if needed.
It is possible to write such an extension using any of the three different extension types:
XUL overlay
Restartless/Bootstrap
Add-on SDK
For the first two types, you would use window.open(). The modal option is in "Features requiring privileges". You will probably also want to look at Window.openDialog().
For the Add-on SDK, you would normally use the open() function in the SDK's window/utils module. Here, again, you will probably want to look at openDialog().
It appears you may be opening content that is supplied from the web in these modal windows. It is unlikely that you will get an extension approved to be hosted on AMO which opens content in such windows which in not included in the add-on release. This does not mean you can not develop the extension and have it installed on your kiosk clients without hosting it on AMO. However, there are additional restrictions in development for Firefox this year which will make this significantly more difficult, see: "Introducing Extension Signing: A Safer Add-on Experience".
You should be able to get similiar window.open behavior, including support for the modal option from the sdk's window/utils module.
You will have to install the onclick listener with a content script, send a message to the addon-main through its port and then open that window from the addon main.

window.name is not persistent in Google Chrome

I'm seeing a strange behavior in Google Chrome, using the window.name property.
For example:
open a tab and go to http://google.com .
Open up console, and type window.name="hello".
Now in the same tab, go to http://chase.com.
In the console, type window.name.
I expect to see "hello" returned, but instead I see "".
Is this a known issue for Google Chrome? It works for me in FireFox.
Anyone have any insight to this behavior?
Thanks!
Update:
If, instead of typing in a new URL, I type window.location="http://chase.com", then the window.name persists!
window is a global object for each document, not for the browser window. In a page with iframes you will have one window for each iframe for example. Each time a document is loaded, a new global object is created and populated for the context. When the document is unloaded, the global object along with all its data is destroyed.
Chrome might start a new process depending on the site.
I would call what Chrome does perfectly acceptable.
You really shouldn't be dependent on any global variable to be persistent. I would consider using session or local storage.

How to open a new window or tab via javascript in windows phone 7 browser

Is it possible to open a new window or tab via javascript in Windows Phone 7 browser?
window.open does not appear to be supported nor does target='_blank'.
Am I missing something here? This basic feature works just fine on iphone and android.
Any ideas on how I can get this working in Windows Phone 7?
On Windows Phone 7 this is not possible programmatically. It's all in the users hand.
To cite a Microsoft employee:
"A user can open a link in a new Tab by tapping and holding the link to
get the context menu but an anchor or scripts request to target a new
window is ignored.
There are several reasons for this:
Cross-window communications are not supported.
Windows Phone only has one instance of the
browser so new "windows" have to be opened as Tab's.
The browser experience is full screen so the user has no good visual cue that they
have moved to a new Tab unless they explicity request it.
Navigating "back" in a new Tab exits the browser which would be
confusing to the user if they did not know a new Tab was created."
If you are trying to add this feature for in-ap browser control, I can suggest you one way.
You have to inject a java-script on every webpage the browser control is able to load the page successfully. In the java-script use window.extern.notify to invoke the ScriptNotify function in your code behind. On the detection of the appropriate message create a new instance of browser control and add it to an array or list. Thereby you can emulate the new tab feature for in-app browser control.
the JS code to be injected may be like this String NEW_TAB_FUNCTION = "window.open = function(__msg){window.external.notify('addnewtab');};";
Which can be injected using browser.InvokeScript("eval", NEW_TAB_FUNCTION);
In ScriptNotify check for addnewtab (keep IsScriptEnabled = True)
void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == "addnewtab")
{
//do work here
}
}
Note that I have overridden the window.open function in the JS with a function which will be injected every time on a new webpage in order to get notified of user input.
Also note this works only for WebBrowser Control and not external browser.
My workaround this issue is simple:
window.open
returns null if failed, so in that case I open it in the same window:
var win = window.open(href, '_blank');
if(!win || win==null){
window.location.href = href;
}else{
win.focus();
}
which is a good practice to have a fallback anyway...

Categories