I am using angularjs.I have link suppose open tab now user right clicks on link and selects open link in new tab ,Now i have code in page abc.html like $window.close();.It is not working as expected i am getting Following Error Scripts may close only the windows that were opened by it.I googled on that i found only browser windows created using JavaScript can be closed using JavaScript.is there any way i can acheive my goal.
Thanks
If the user has chosen to open it in a new tab, you can't close it with JavaScript. However, if you use window.open(), you can close the opened window using JavaScript.
So the answer is no, unless you hack the user's computer.
It is possible.
try this:
window.top.close();
Working for me on all three major browsers.
if it does not work try :
open(location, '_self').close();
Related
Before anyone marks this question as duplicate, I would like to say that all the other answers are from 2014 or older. The workarounds or hacks do NOT work anymore on the updated versions of Chrome.
I have a logout page that needs to be closed upon clicking a link. I have read about how Javascript cannot close the window if the same script hasn't opened the window.
I am currently using Javascript to achieve this. How can this be done? (Basically, shut the tab using Javascript).
I have tried the latest answers such as:
var win = window.open("window.opener.location", "_self");
win.close();
which don't work anymore! Any help is appreciated.
You can only close windows/tabs that you create yourself. That is, you cannot programmatically close a window/tab that the user creates.
For example, if you create a window with window.open() you can close it with window.close().
I want to create elements which (when clicked) will guide visitors towards two differenct websites (both in new windows). How can I do this? Below are the two ways which I tried, which both do not work as only one page/ window is opened. Thanks in advance!
Timo
The solution as suggested here: How to make a link open multiple pages when clicked . But maybe the solution is outdated, but for me only the first webpage is openend (with all the indicated solutions. (for example the code which is suggested there)
HTML Click Here
$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('https://www.youtube.com/watch?v=H9XIXFwpyEc');
window.open('https://stackoverflow.com/search?q=open+multiple+pages');
});
While both using a class and an id with both a different a href and both with the extra options of opening new window also does not work as it only opens the first window
HTML Click Here
$('.yourlink').html(''); // Add page
$('.yourlink a').attr('target', '_blank');// Make sure it opens in new window
$('#extra').html(''); // Add page
$('#extra a').attr('target', '_blank');// Make sure it opens in new window
Using Chrome, I'm not seeing the problem you describe. But when I tested it in IE, I suddenly got a "popup blocked" message.
It's not strange though. I'd hate it if a single click on a link could suddenly spawn 10 new windows. In this scenario I actually think IE handles it better (by blocking the second window).
The thing is that window.open will only work if the action that invokes it is a trusted event. That usually means a user-initiated event, like click. But what Chrome doesn't account for (I assume) is that a single trusted event can then invoke several window.open.
I've tried to work around this feature, but have not (yet) been able to fool IE. The options, as I see it, are:
Ask users to add your site to the popup exceptions (internet settings).
Spawn the pages in iframes within your own site
Ask users to use another browser ;)
Or the obvious:
Use separate links for the windows
Have looked at the answers to previous questions and am not sure if it
is the same issue as I am having. Some of the possible answers I have found
on the net are aged so am not sure whether anything has changed regards this.
I have this code to check for a cookie and then either open one or two
tabs dependent on whether a cookie is found.
On the html side I have a link like this:
<a target='_blank' onclick="newTab()" href="http://www.test.com">
In the head section the newTab() function does the cookie check then
if needed opens a window like this:
window.open('http://www.test.com/members/','_blank');
When the link is clicked the above works fine in Firefox and IE and both
windows open when needed but in Chrome only the onclick tab opens and not
the main url regardless of the cookie.
I imagine this is an issue relating to opening tabs/windows in Chrome but
I thought that the above would be dealt with by Chrome as the opening windows
are the result of a user click.
Am I missing something simple or is it a restriction on Chrome?
Thanks for any advice.
Indeed as answered by CBroe the reason for the problems was because
I had used _blank as the window name for both.
I changed one over to _parent and it is at least working like this now
so thanks for the assistance.
I'm brand new to StackOverflow and userscripts, and I'm trying to get a fresh start!
Worthless information aside, I'm having a bit of trouble with a tiny script I recently whipped up.
(function () {
$("#enbut").click();
setTimeout(function () {
open(location, '_self');
window.close();
}, 100);
})();
What it does is clicks a button, waits a second for the website to register that I've clicked it, and then it closes the webpage.
The only issue that I am having is that it does not seem to want to close the webpage when the tab isn't the one I currently have active (Opened up so I can see it).
The script works fine if I open the webpage directly, but if I right click and open in a new tab, it's able to click the button, but the page doesn't close until I open up the tab.
Is there any reason this would be happening, or any way to fix it?
I'm using Chrome, so maybe it's just a browser security feature like what they have done with closing windows?
window.close(); throws a security issue when used as a general userscript in today's browsers. Even when using the work around ( open(location, '_self'); ), it does not seem to allow it in tabs that are not the active window.
In order to resolve this, I had to convert the userscript to a Chrome Extension, which gives the script full control over Chrome's security measures. I don't think there would be any other way to get this working as a plain userscript without messing with Chrome's internals, which would be a stupid thing to do for a simple script.
Thanks for the help, guys!
How to avoid the confirm dialog box “the web page you are viewing is trying to close the window” when trying to close IE popup?
Make sure the window you are trying to close is one you opened with JavaScript, and not the one the user arrived at your site with (complete with history they might want to go back through).
This hack used to work in IE, but not sure if it still does:
window.opener = window;
window.close();