I am using IE8 on Windows 7. Referred to several threads and understand that in IE8 when I am using window.open to popup a new window, the JavaScript window.open is returning null value.
If I run IE as administrator or disable the protected mode, I see the window.open returns the expected object.
I am looking out for a solution apart from the options mentioned above. For such a small feature (opening a popup) I cannot ask customer to run IE as administrator or disable the protected mode.
If there is any work around, please let me know. It will be a great help.
Primarily, I want to make sure that only one window is opened when user clicks multiple times on the link and give the focus to the window which is already open. To achieve this I need to get the object from window.open so that I can check if the window is already open and give the focus to the already opened window. Otherwise open a new window.
For IE10, window.open returns a NULL reference object if Enable Protected Mode is checked under Internet Options->Security->Security Level for this zone and the ZONE is different i.e. in my case local file opening a popup from Intranet.
window.open returns a reference object even if Enable Protected Mode is checked when yoursite.com opens someothersite.com in popup window i.e. Internet->Internet
You can use window.showModalDialog as a alternative or replacement for window.open method.
It is more secure then window.open. It will not allow user clicking the Parent page.
Example Usage:
var myFeatures = "dialogWidth:1060px;dialogHeight:550px;resizable:yes";
window.showModalDialog(url,window,myFeatures);
//Here window is an object, no need to assign or declare.
If you want more detail explanation see Here.
//Fifth Question.
Related
I am opening a new window with window.open(). If I do NOTHING else on this page, and click the "close window" link, the window closes. This works perfectly; however, if I navigate between pages(all under the same domain) window.close() no longer works.
Is there a way to fix this?
Here is how I am opening pages in this example...
<a href="###" target="_blank">
Here is my close link:
close
I use 2 methods of changing pages within the opened windows.
<select onchange="if (this.value) window.location.href=this.value"> AND STANDARD <a href="####"> tag
Do I need to navigate links within this window a certain way to still maintain my window.close() ability?
You should favour using window.open() to open a new window if you want to close it using window.close(). E.g.
<script>
function newWindow() {
window.open('foo.html', 'myWindow');
return false;
}
</script>
link
From MDN:
The Window.close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.
I understand the behaviour you're describing where you can close the window as long as you don't navigate. I can replicate this in Google Chrome.
I believe this is because (from the spec):
A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.
Your new window is considered a "top-level browsing context", not an "auxilliary browsing context". Up until the point that you navigate, the history contains one document, so can be closed using window.close(). As soon as you navigate, the history has more than one document so it's not valid to close the window.
You should also have a read about window.opener and the security risks it poses.
window.opener provides a reference to the window object that opened the current window.
Recent advice is to use rel="noopener" on all links that open in new windows. Again, this is because setting window.opener without knowing what you're doing poses a security risk. Have a read about it here:
Links to cross-origin destinations are unsafe
Browsers are now starting to treat all target="_blank" links as if rel="noopener" had been set, even if you as a developer don't set it. You can see that this is now in most major browsers.
Therefore, you could use link (explicitly setting window.opener) and I think you'd get the behaviour you want. However, this might not be reliable across browsers, and also has security implications as described in the web.dev article.
According to many rules and security features, window.close() will only work in specific cases:
From the latest working spec for window.close():
The close() method on Window objects should, if all the following conditions are met, close the browsing context A:
The corresponding browsing context A is script-closable.
The browsing context of the incumbent script is familiar with the browsing context A.
The browsing context of the incumbent script is allowed to navigate the browsing context A.
A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a browsing context whose session history contains only one Document.
I have a web application that allows users to close new windows and it works fine, except when the rules above are not respected.
What I am looking for is to detect when the close() function will work and only show the close button in such case.
I found information talking about window.opener that returns a reference from the window that opened it. But it doesn't work.
if(window.opener != null){
//show button
}
Maybe this is because the new window was opened using "right click -> open in new tab" and not a script. When tabs are opened in this fashion window.close() works, I just want to detect when window.close() will work.
Any ideas?
According to the docs, the window is script-closable also if session history of the given context is of length 1 (which is exactly what happens when you open a link in a new tab/window). You need to add that to your checker.
if(window.opener != null || window.history.length == 1){
//show button
}
As I understand it, there isn't a native way to do this. It is possible to check if window.close() has failed after the fact by checking if window.closed is false to detect an error. See here.
Your only option is to offer the functionality to close every window, or try to in all cases, and ask the user to close it manually if it fails - otherwise it cannot be closed by you, or any of your code, by definition. There isn't a way around that unfortunately. One option that might be worth trying would be to redirect them somewhere if the window cannot be closed programmatically, which you can verify easily after every attempt.
I want to make a window with HTML that works similar to ones opened by windows. I know the method with actual browsers, but it isn't good enough as I have link and navigation buttons.
This would make my job easier in making softwares with lots of animations
The best you can get is calling Window.open with third argument set as: 'menubar=no,location=no,resizable=no,scrollbars=no,status=no'.
This will open a new browser window with only the address bar shown.
There is no way to open a native window out of the browser's scope from JavaScript code other than this. It is a security limitation.
However, the other alternatives include are Window.alert or a Window.prompt.
If you want to open a popup box you can use alert("This is a dialog box!");, confirm("Is it ok or not?"); or prompt("Enter a value and confirm it or not");.
https://www.w3schools.com/js/js_popup.asp
If you want to open a native window, that is not possible as it has already been said.
I have a web application where page #1 opens a popup window using
window.open(myUrl, "fixedApplicationTargetId", "");
Then page #2 overwrites the same popup window with a call to window.open using the same target value
window.open(anotherUrl, "fixedApplicationTargetId", "");
At this point the content of the popup originally created by page #1 shows the new content created by page #2. So far so good with any browser.
Then the popup itself detects who last opened the popup and updated the content using window.opener. Prior to calling window.open both page #1 and page #2 create a global variable globalPageId and assign a unique number each. The popup checks the value of window.opener.globalPageId and detects which window last updated the popup content.
This is where things fall apart: the above works fine with chrome and firefox that update window.opener in the popup each time the content is updated with window.open. Instead, IE and opera always point the popup window.opener to the first window that used window.open.
Any suggestion, in a context where multiple pages call window.open on the same target, how to detect from the popup itself which window last opened the window?
window.opener is supposed to be read-write (except in Internet Explorer 3), so you could set it to the appropriate window yourself. Some browsers, however, restrict this operation and only allow setting opener to null to prevent security issues.
An alternate solution would be to use a custom property instead of opener. You could set it by hand:
window.open(myUrl, "fixedApplicationTargetId", "").realOpener = window;
Then use window.realOpener.globalPageId instead of window.opener.globalPageId in the rest of your code.
I have access to a joomla edition but not the source, and I need add a link which must be opened in other window/tab.
I only can change the href field content.
I try with
javascript:window.open('http://url/')
this work with chrome but not with firefox.
any suggestion?
According to MDN, window.open() requires a minimum of two arguments. It also returns the object for the newly opened window, which would cause [object Window] to appear in the original window. Returning undefined avoids this second issue. This would work:
javascript:window.open('http://url/', '_blank');undefined
Older code would commonly use the void operator, which evaluates its only operand and yields an undefined result no matter what. So this would also work:
javascript:void(window.open('http://url/', '_blank'))
In either case, the special window name _blank just tells the browser to open a new window or tab. The browser's settings determine the exact behavior because tabbed browsing was introduced after _blank was.
Just use a _blank target attribute, which opens the link in a new window/tab (you can't control which one, the browser will pick based on a click event algorithm):
Link
Not sure why that wouldn't work in FF using the "javascript:" protocol but the JS, in a regular script tag, would probably work. The MDN entry on window.open might be helpful to read as well.