I have one strange issue as below for Chrome in iPad.
There is the web page, which call window.open and window.close to select the user and close. H/w when in 1st Tab page, it can successfully call window.open to open the page to select user, but when click select to call window.close, it does not work without any change, still stay at the current screen to select user.
But if I open the web page in 2nd, 3rd...Tab page, it can both successfully window.open and window.close to complete the user select process.
Meanwhile, if I clear all browsing data and close all tab pages, then the new 1st tab page will works with window.open and window.close.
But if I not only clear all browsing data, but also force close Chrome (double click home button and swipe Chrome out), then the new 1st tab page will fail with window.close.
Another interesting find is that if I open such as google.com at the 1st tab of Chrome, then in 2nd tab still open my web and call window.open and window.close, all of them also works.
May I know if there is any difference about the 1st tab of Chrome or something else wrong with window.close for Chrome in iPad.
Finally I find possibly issue root as opener is null at the 1st tab.
Could you pls help to check and thx a lot!
function doSelect( userid, username )
{
opener.document.`formName`.`fieldPrefix`_ID.value = userid;
opener.document.`formName`.`fieldPrefix`_Name.value = username;
if ( opener.document.`formName`.`fieldPrefix`_SavedName )
{
opener.document.`formName`.`fieldPrefix`_SavedName.value = username;
}
if ( opener.markDirty != null )
{
opener.markDirty();
}
window.close();
}
function chooseUser( prefix, title, filter ){
window.open("/livelink/livelink.exe?func=user.SelectUserDlg&formname=ReportPrompts&fieldprefix=" + prefix + "&title=" + title + filter + "&DisplayUserName","","height=340,width=680,scrollbars=yes,resizable=yes,menubar=no")
Because of abuse in the early days of JavaScript, browsers will only let you window.close() a window that you created via JavaScript or a brand new window.
http://www.w3.org/TR/html51/browsers.html#dom-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
responsible browsing context specified by the incumbent settings
object is familiar with the browsing context A.
The responsible
browsing context specified by the incumbent settings object 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 top-level browsing context whose session history contains only
one Document.
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 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 this code which opens all links in my page in a new window:
<base target='_new' />
It works fine in Chrome but in IE(8) and Firefox not so. In Firefox it does open a new tab, but on a second link click it loads in the new tab but without putting this tab up front, so a user would have to click on the new tab manualy. In IE it opens a new browser window. Is there an equivalent code (..js/Jquery) to open in a new tab in all browsers?
there's no guarantee where the browser will open that new window/tab. different browsers open new windows/tabs differently, and that behavior can also be affected by browser settings.
Firefox has an option to switch to the tab immediately.
Tools -> Options -> Tabs,
"When I open a link in a new tab, switch to it immediately"
IE9 has the same option
Tools -> Internet Options -> General -> Tabs Settings
"Always switch to new tabs when they are created"
IE9 has the option of what to do with new popups.
Tools -> Internet Options -> General -> Tabs Settings
"When a popup is encountered"
- Let Internet Explorer decide ...
- Always open popups in a new tab
- Always open popups in a new window
From http://windows.microsoft.com/en-US/windows7/Tabbed-browsing-frequently-asked-questions
If you opt to let Internet Explorer decide how to display pop-ups, it
will display the pop-up in a new window if the pop-up specifies size
or display requirements. Otherwise, the pop-up is displayed in a new tab.
So the behavior is mostly left up to the user and not the developer.
You can not control this part (how to open - in tab or in window). Since this is decided by browser. More of that, you can not even rely on type of browser, since each user may select his or hers way to open new pages: always in tabs or always in new windows or some other way.
I'm not sure this applies with the "base" tag, but on links, the "target" attribute can either have one predefined keyword, or any name you want to give the new window.
The available keywords are:
_blank: opens the links in a new window or tab
_self: opens the links in the same frame as it was clicked (this is default)
_parent: opens the links in the parent frame
_top: opens the links in the full body of the window
(http://www.w3schools.com/tags/att_a_target.asp)
If you don't use one of those keywords, you can use any name you want and this name will then be used to refer to that window. This allows you to reuse a tab you opened to load a different document in it.
So by using "_new" (which is not a keyword) as the base target, you essentially say that all links must be opened in the window named "_new". At first this window does not exist, so the browser creates it (first click), and the it reuses it for all following clicks.
Use "_blank" instead so that each link opens in its own new tab.
I have a link which opens a page in a new tab in firefox.
<a target="_default" href='/portal.html' >
Go to Portal
</a>
However when I click this link again ,it refreshes the opened tab and doesn't set focus to it , so users have no way to know that tab is opened .
Is there any way by which I can grab the opened tab and set focus on subsequent clicks after opening it once Or any other workaround .
Thanks.
For getting the focus using a tabhandler name may help.. suppose we have
win = window.open("https://www.google.com", "test");
win.focus();
now every time the first line of above code runs the browser will first find the tab with the name test and reload it with the url https://www.google.com and in case it does not find a tab with the name test it will open a new one. On the run of the second line it will set the focus to the same tab loaded from the first line.
I had a similar problem in my project.
Luckily, whenever I needed to set focus on already opened tab, it also needed to be refreshed.
So I did it with the following trick, which first opened again tab with same href and target, got reference of that window back, closed it, and opened again with same parameters.
popup = window.open('/popup.html', '_popup')
popup.close()
popup = window.open('/popup.html', '_popup')
No.
Each tab normally runs in a separate process sandbox. It's just not possible.
At max you can try target="_blank".
You can probably use _blank and some JS code that closes a previously opened tab for the same URL using cookie tracking.
The page (that you are opening in the new tab) should be built in such a way that it writes out a cookie with some value (say current time in millis). It should also have some kind of a periodic poller that checks the value of this cookie and closes itself when the value is seen to be changed. the only way this value can change is when the same url is opened in another tab/popup.
Disclaimer: Not sure if browsers will allow window.close without it being user triggered though!