Close the current tab and open new tab using javascript - javascript

Close the current tab and open a new tab using JavaScript. On Chrome or any other browser.

Try this:
window.open('http://www.google.com','_blank');
window.setTimeout(function(){this.close();},1000);

Close the current tab
You can't close the current tab until and unless it has parent
reference
Its simple, if the current tab is opened by self I mean, you went to browser and typed the url in the url bar can't be closed with the javascript. Because of security reasons browsers will not allow these type of things.
If you try the following code
window.close();
you'll get following warning message by browser
Scripts may close only the windows that were opened by it.
If the tab is opened with window.open, right click on the link and opened on anchor tag then following should work
window.close();
self.close();
Opening new tab using the javascript :
window.open("https://stackoverflow.com");

Related

closing a new browser tab using jquery / javascript

A new tab opens up when i click a link given in a iframe. Is it possible to close these newly opened tabs by giving some controls in the iframe
Most modern browsers sandbox each tab, preventing the ability to do what you're requesting. As an example, run this jsFiddle and watch your JS console in your browser.
The code will open a new browser tab using JS so we have a little bit of control over it:
var x = window.open('https://www.google.com');
But, when we try to close it:
x.close();
In most browsers you will see an error when you attempt to control navigation or close the window in the second (sandboxed) tab.

Open new tab in browser window (opened with window.open)

I have a new window opened via window.open method and in that window in content I have a link with "target=_blank".
When clicked the link opens new tab in parent window instead of the one that content was in. I also noticed that for that window (created with window.open) the Ctrl+T shortcut (to open blank tab, Firefox) does not work.
I checked window.open specs and I didn't find any option that would allow this behavior.
var a = window.open("http://www.google.pl",
"Test",
"width=640,height=480,scrollbars=yes,toolbar=yes,menubar=no,location=no,resizable=yes");
a.focus();
Setting toolbar=yes makes it work in Firefox. Any idea how to make it work in chrome?
Could you show some code?
else; try with javascript:
function OpenNew(){
window.open("http://www.google.se", "_blank");
}
Rgds

How can I open a pop-up window without it being blocked?

I haven't found a single answer able to tell me what's the right way to open a popup.
Times have changed, and popups have been mostly replaced with fancybox-like boxes. However, there are still times when popups are needed.
For those cases, I don't want my popup to be blocked by the browsers.
What's the right way to open a popup without it being blocked? Even if it opens a new tab in the browser. I just want my popup to be open, and have control of it from the parent or vice versa.
Popup blockers will block any popup, unless it is opened because of an user action.
If the user clicks on a link, and a popup is opened in the click listener of that link, the popup blocker knows the user want to open something and will not (or should not) block the popup.
What you cannot do:
open a popup when the page is opened or closed
open a popup after a certain interval
open a popup after something asynchronous happens
What you can do:
open a popup in the on click listener
using target="_blank" in a anchor tag
You can access both windows with JavaScript variables:
if you use window.open, the parent can have a reference to the popup by assigning the result of window.open to a variable. Check out this article at W3Schools.
If the popup needs to have access to the window who has opened it, you can use window.opener. Check out this question.
try this, it works for me
$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
Js fiddle for this is here https://jsfiddle.net/safeeronline/70kdacL4/2/
if you want to open new tab after ajax call see this fiddle http://jsfiddle.net/safeeronline/70kdacL4/1/

Close currently opened tab

I've tried window.close(); to close the currently opened tab but it does not seem to work. What am I doing wrong? My scope is to run a JavaScript script that automatically closes a manually opened tab.
Thanks.
Typically in javascript (or other flavors of javascript), this is done with...
Close Window
$(".close").click(function {
window.close();
});
Note that this will only work on the tab that contains the link that was clicked. It will not close other tabs open in your browser.
I'm giving a +1 for each comment on the question, too.
You can have a look at this answer how-to-close-current-tab-in-a-browser-window
Have a look also at the comments of this answer, where Ryan Joy says:
"This method is only allowed to be called for windows that were opened by a script using the window.open method." In other words, you can only use JavaScript to close a window/tab that was spawned via JavaScript"
Also notice that:
window.close();
works only for Internet Explorer where a popup message appears asking if user agrees for current tab/window to be closed.
In Firefox, Chrome only works for empty tabs and tabs/windows opened via "window.open()" method as mentioned above.

JavaScript close tab after window.open()

I've got a page which open a new window fullscreen but it still has the other page (the one which re-directs) behind so when you close the new window you are taken back to a blank page.
Is there a way to open a new window and then close the tab which is now inactive?
Currently I have some javascript like this
window.open("http://website.co.uk");
Thanks
You can technically use window.close() on the opening tab, but browsers usually will not allow JavaScript to close windows that it has not opened. Can you redirect to some useful page that the user can use once they have closed the window?
If you can add some JavaScript code to the page being opened, I think you can use window.opener.close() to close the original window. I tested it in IE8, the browser will ask for confirm before close the original window.

Categories