I am trying to close the tab using script, but it's not working.
<button type="button" class="btn btn-primary" id="btnClose">Close</button>
$('#btnClose').click(function() {
var newWindow = window.open('https://localhost:44384/MasterUpdate/Index/1', '_self', '');
window.close(newWindow);
});
The URL I am using is for testing purposes only. In my code I manually manage it using this:
string referer = Request.Headers["Referer"].ToString();
ViewBag.GetCurrentURL = referer;
I have tried so many ways but they didn't work for me.
javascript:window.open('','_self').close();
window.top.close();
window.close();
As Rory pointed out, there are few conditions when window.close() works.
Below is the excerpt from this link:
close browser tab/window
Note that window.close() will only work under the following conditions:
On all modern browsers, the web page to be closed must be the first in that window/tab's session history. That is, if you hit the Back button on the browser, you will not go to a previous page in that window/tab because there is none.
You can easily accomplish that by opening that page either with JavaScript, for example, by using window.open(), or through a link that has a target="_blank" attribute. You can find example code for the former in the demo below, and the latter in the article on opening links in a new window or tab.
If you only test your code in an old browser (eg, old versions of Chrome or Firefox, or any version of Internet Explorer), you will end up with the mistaken impression that it works fine even if the above conditions are not true. Newer browser versions impose these restrictions for security (and other) reasons.
Modern browsers will also resist your attempt to trick them into thinking that an existing window/tab was opened with JavaScript when it was not. Some older versions fell for such trickery, but these methods should no longer work in the current versions of Chrome and Firefox.
Also, for tab/window must be opened via window.open() for window.close(0 to work.
If you will to close the tab you've created with window.open you just have to store the new tab into variable and then use .close() method of this variable when you want to close it.
Short example:
const newWindow = window.open('');
setTimeout(() => newWindow.close(), 1000);
This example shows how to create new window and then close it after 1 sec.
If you want to manualy close the tab, just call newWindow.close() function wherever you want.
I have an alert box, and when I run that code in Firefox, no matter what program I have over firefox, firefox appears with the pop-up automatically over other programs open.
This however, does not work in Chrome.
What gives? Would this be a perference change or this there a line of code that says focus on this window.
I used
window.blur();
window.focus();
and that fixed my problem :)
The implementation of alert is browser specific. Unfortunately there is not line of javascript code to change this.
If you want to have customizable (and working the same in every browser) alert window, use jquery UI.
Note: It will be over the actual page, not over the (other) window.
I am adding the following javascript at runtime to open a new browser window. I know window.open is available in javascript. But it doesn't work for safari and chrome. Is there any other trick or hooking technique to achieve this.
string script= "window.open('http://www.w3schools.com','_blank','toolbar=yes,location=yes,directories=no,status=1,menubar=yes,scrollbars=yes,resizable=no,copyhistory=yes,left=20,top=20,width=600,height=550');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindowScript", script, true);
Thanks
window.open() most certainly works in Safari and Chrome, I used it today! The demos on the w3Schools site work in Chrome
Try moving the code into a standard in-page <script> block (obviously with the .net stuff removed) to test and see if you get any errors in the console.
It could be that one of the features you are passing as the 3rd argument to .open() is breaking things (like the copyhistory feature - I never saw that before, and it isn't listed in the available options on the w3Schools site)? I have noticed that Chrome tends to ignore the width and open it to whatever size it wants to...
This is because of the popup blocker in the browser. You can use Ajax Modal Popup window instead of Window.Open
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ModalPopup/ModalPopup.aspx
Another option is Jquery popup
http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/
if you still need to use Window.Open then please refer this
Bypass popup blocker on window.open when JQuery event.preventDefault() is set
I really need your help. I am working on a pop up window
using JavaScript.
I am using the window.open(URL,name, properties);
I wanted to load a window that does not load the URL window.
I tried the location=0 and location=no setting, it seems to work
fin on Firefox but alas it is not working on IE!
In Firefox, it is not showing the location bar anymore, but in IE
the location bar is still there with the URL!
Please help. :9
tinks~
You seem to be calling the method correctly, IE most likely doesnt allow you to hide its url bar. Most browsers have user-defined preferences whether they want to allow websites to be able to hide toolbars. Popups are very susceptible to security options.
I'm afraid you'd have to live with it, if the user doesnt want you to hide the url it wont.
Not possible anymore as all modern browsers do not allow you to hide the URL in Popups anymore :)
This is the JavaScript I'm currently using:
window.open('/modules/mod_oneononechat/chatwindow.php?key='+key+'&color=blue','x'+winName+'x','location=0,status=0,toolbar=0,menubar=0,resizable=0,scrollbars=0,height=375,width=420');
This doesn't seem to be working in IE8.
It's a chat window that works fine on all other browsers (including IE7). Any ideas as to why it's not working on IE8?
IE8 doesn't like spaces in the window name.
Removing the space from the window name solved the problem.
In IE8, the below function was not opening a new window pop up whereas it was working perfectly in Mozilla and Chrome.
function openReports(reportUrl){
window.open(reportUrl,'Report Information','height=800,width=1000,left=200,top=200,toolbars=no,resizable=no,scrollbars=auto,location=no');
}
Removing the space (Report Information)from the window name solved the problem for me too.
This works for me:
javascript:window.open('http://google.com', 'x'+'winName'+'x', 'location=0', 'status=0', 'toolbar=0', 'menubar=0', 'resizable=0', 'scrollbars=0', 'height=375', 'width=420');
I too had a problem with this issue. I had written a function like
function newPopup(url) {
popupWindow=window.open(url,'Detailed Informations','height=700,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
where there was a space in the window name i.e "Detailed Information" and due to this the popup was not working. Just dont put any spaces in the window name.
window.open has three parameters: url, windowname, otherfeatures.
For FF, if the second parameter (windowname) is not passed, it still works :)
But in IE 7,8 which we tested, we have to pass the second parameter
var newWin = window.open('', '', 'width=400, height=400, top=100, left=100');
In IE8 Use This first two arguments has to be blank then it works on IE8.
Can you check whether it throws any javascript error?
You can use IE8 built in javascript debugger
The support article titled Q281679 by Microsoft was released for MSIE 5.5/6.0. But it could be applicable for MSIE 8.
Alternatively, you could use the X-UA-Compatible meta tag or header, and see if this can be resolved by resorting to the compatibility mode (I would personally use this as a last resort, and would instead attempt to write JavaScript that does not cause IE8 to behave as described).
PS: To avoid IE8 from requiring the compatibility mode, use the services of the W3C page validator, and have the page tested in another standards compliant browser like Firefox 3 (don't forget to use Firebug and the Web Development Toolbar extensions if you use FF3).
If you use spaces or dashes in the window name, IE won't work (at least some versions that I used).
IN IE if you open one window with window.open() - again if you want to open new window with window.open(),It will not open new window.
For this the solutions is - IN Window.open parameters pass this parameter "copyhistory=no".
e.g:
WindowName = window.open('','Name','height=320,width=428,toolbar=no,
menubar=no,scrollbars=no, resizable=no,location=no, directories=no,
status=no,copyhistory=no');
Hope this will be helpful for you.