How to close a window using jQuery for all browser compatibility?
<input type="button"
name="backButton"
value="Close"
style="background-color:#245f91; color:#fff;font-weight:bold;"
onclick="window.close();">
This onclick event is working in IE. Could you help me to write code in jQuery?
$(element).click(function(){
window.close();
});
Note: you can not close any window that you didn't opened with window.open. Directly invoking window.close() will ask user with a dialogue box.
just window.close() is OK, why should write in jQuery?
You can only use the window.close function when you have opened the window using window.open(), so I use the following function:
function close_window(url){
var newWindow = window.open('', '_self', ''); //open the current window
window.close(url);
}
For IE: window.close(); and self.close(); should work fine.
If you want just open the IE browser and type
javascript:self.close() and hit enter, it should ask you for a prompt.
Note: this method doesn't work for Chrome or Firefox.
This will only work for windows which are opened by using window.open(); method. Try this
var tmp=window.open(params);
tmp.close();
Related
From my webpage, I am opening a new page in different tab. When the page in different tab will be loaded, I want to close my webpage i.e. Suppose I am on pageA and I opened pageB using window.open(). Now, when pageB will be opened, I want to close pageA. I tried this jsFiddle -
function onClickBtn()
{
var win = window.open('http://www.google.com','_blank','');
setTimeout(function () {
win.close();
}, 5000);
};
This is my HTML markup -
<input id="btn1" type="button" value="Click me" onclick="onClickBtn()"/>
However, this code is closing pageB ,not PageA.
I have tried a jsFiddle https://jsfiddle.net/gdso9eeg/
Please suggest me a suitable solution.
It is doing what it should do.
You opened the new window with that variable and on using close() with it, it is closing the window which it opened.
If you want to close the parent then open the new page on the parent.
Try this:
In place of _blank put _parent
If page is redirecting on the same page window then window.close will not work due to some browser security for this scenario type we need to use one of the following way.
1.window.history.back();
2.document.referrer
3.Request.UrlReferrer server side
The .close() should follow Javascript's window, not win.
Try
window.close();
Good luck!
EDIT:
Have you checked this post window.close and self.close do not close the window in Chrome ?
"..javascript must not be allowed to close a window that was not opened by that same javascript."
It also offers some workarounds to the issue.
I have to close the current tab when clicking on a button. My current code is given below:
function isCloseWindow(){
window.opener = null;
window.open('', '_self', '');
window.close();
}
You can't close the current window from a script, only those, you have opened. There are some (mostly not working) hacks for this, but they are just that hacks.
I want to create a link that says 'Close' and it should close the window. Anyone have any idea how to do this?
I've looked at other people's questions and it seems I can't find anything that works.
This is what I have right now:
<a id="noThanks" href="JavaScript:window.close()">No, thanks</a>
There is javascript command for closing current window.
window.close();
or if you are opening named window
function openWin() {
myWindow = window.open("http://www.domain.com", "_blank", "width=200, height=100");
}
function closeWin() {
myWindow.close();
}
Using win.close() will close the window if win is a variable that has the value of a window.open() call stored in it.
So,
var win = window.open("//stackoverflow.com");
win.close();
will close the window opened by window.open().
However, window.close() only works on a window that was opened by JavaScript. That is, you can't close a window that a user navigated to by clicking a link.
To control a window, you'd have this:
Open Window
Then, in your new window, you'd have this to close it:
<button onclick="win.close();">Close</button>
You can only close a tab/window that you've opened using window.open
Similar question and answers can be found here
This is the answer I was really looking for in case anyone else ever needs it.
Just a heads up--this doesn't work in FF.
window.open('', '_self');
window.close();
In my case once data saved I need close window after confirmation .
<script type="text/javascript">
if (confirm("Close Window?"))
{
close();
}
</script>
Don't forgot to call jquery library before your script
I have a button:
<button>Click</button>
I want click it and open a new window in the same page, I bind the click event an event handle:
$('button').click(function () {
var newurl="http://" + window.location["host"] + ";
window.open(newurl, '_parent');
})
But is always open in new tab or new window, the second parameter I have try _self _top.
I even have try window.location.href(newurl)
So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.
Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.
Update
OP has cleared the confusion of all through his comment below his question, this is what you need to set a new url for current window:
$('button').click(function () {
var newurl="http://" + window.location["host"];
window.location.href = newurl; // or simply window.location
})
window.location =newurl; will open a new window replacing the parent
Have you tried this?
<script type="text/javascript">
window.open ('test.htm','_self',false)
</script>
If you mean you want to replace the current page, window.location = newLocation; will do.
If you want a modal popup, try JQuery UI Dialog.
i want to open a window in a new tab, but when i want that opener page to be active, not the new one. How can i do this... Many thanks
my code is something like this :
<script language="javascript">
window.open("http://www.google.ro");
window.opener.location.focus();
</script>
To give focus to the new window (but you don't want that, and it will probably have focus by default):
var newWindow = window.open("http://www.google.ro", '_blank');
newWindow.focus();
I don't think it's possible to steal the focus from the new opened tabs. I didn't find any official statement telling this, but all the articles I found on this subject talk about configuring your browser to open tabs by default without focus.
The only "solution" I could come up with is this one:
window.open("http://google.com", "_blank");
window.alert('Hello there! This is a message that annoys you, the user.');
Note that it is possible to shift focus when opening popups.
Learn more about the window object.
This works for me...
var newWindow = window.open('http://www.google.ro', '_blank', 'height=300,width=300,menubar=0,status=0,toolbar=0', false);
window.focus();
To give back focus to the opener window, try using window.focus(window.name); instead of window.opener.location.focus();