I'm working on a asp.net project and I need to find a tab in a browser by the URL and close that tab... is there a way to do this by c# or scripting languages? the solutions that works even in one browser (i.e. firefox) is enough for me. thanks.
-
This is certainly not possible in Javascript, for what it's worth. – Jake Oct 16 '14 at 22:06
-
JavaScript cannot close other tabs due to security issue. Besides, you should not close client's browser or tab from your code. What is the reason behind it? – Win Oct 16 '14 at 22:14
-
1You could close a tab opened by your own website, but that's it. Is that what you're looking for? – Saeb Amini Oct 16 '14 at 22:19
-
Seab Amini, Yes exactly but the link that I open in another tab is not from my site. – Sara Nikta Yousefi Oct 16 '14 at 22:28
-
Win, I need to do this because my client asked to see the urls in the database but he wants to close tabs from my website not with the browser – Sara Nikta Yousefi Oct 16 '14 at 22:31
-
1I'd hate it if some tab just closed on its own!!! – frenchie Oct 16 '14 at 22:54
4 Answers
It depends to your database.
You can create one value in database with a string null and the page have a timer and verify this string.
IF(string == HttpContext.Current.Request.Url.AbsoluteUri)
response.write("<script>close();</script>");
You understand?
- 3,764
- 1
- 20
- 25
-
-
not quite...I tried the close(); before but nothing happened...maybe I used it wrong...this is the code I tried for the first time...string script = " "; ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", script, false); – Sara Nikta Yousefi Oct 16 '14 at 23:04
-
1see if your "inspect element" have any error! And try with: string script = " window.close(); "; – Diogo Cid Oct 16 '14 at 23:15
-
maybe this link help in your ScriptManager: http://stackoverflow.com/questions/4994040/scriptmanager-registerstartupscript-code-not-working-why – Diogo Cid Oct 16 '14 at 23:22
-
-
No...I tried window.close() and I checked it with firebug...the javascript code added to the html but simply nothing happens...I wish I had a clue – Sara Nikta Yousefi Oct 18 '14 at 13:11
-
in Firefox you have to go about:config and change allow_scripts_to_close_windows for true and the close() work :\ – Diogo Cid Oct 19 '14 at 14:34
finally I come with an answer...tnx for those who helped me find out how to solve this...
the trick is to keep the id of the page you open...
var win;
$('#click').click(function() {
win = window.open('http://www.example.com', "something");
});
$('#Cclick').click(function() {
win.close();
});
function closeit() {
win.close();
}
of course I think no one like to use it this simple but to understand it well this is the best way... if we have two link one with the id of "click" and another with the id of "Cclick", by using the code above in the jquery part we can see,clicking on "click" link opens a tab(its like choosing _blank for target in simple link) but as shown in the code we hold the id of the tab in a variable called win, so when we click on "Cclink" link win is going to close... by combining this with the c# code we can open different url and close them as desired...
- 1,467
- 5
- 21
- 35
If you are talking about closing the client's tab from the server side then the answer is NO you can't. Due to security issue mentioned by Jake and Win, JS can't close the tab (it can't even tell the browser to open up a link in a new tab). As for the c# server side, all it can do is send some data to the Client and have the Client to render them. There is no such thing as close tab code so this won't work also. The closest you can do is to close the current page but that doesn't fit into your requirement of close by URL.
- 11,696
- 7
- 43
- 81
-
but my client showed me a website that could do that .... I'm not aware of how but I saw it functioning – Sara Nikta Yousefi Oct 16 '14 at 22:34
-
@sara.y mind tell us in detail of what you saw? maybe its just an illusion – Steve Oct 16 '14 at 22:35
-
of course...it not an illusion actually the site is quite famous the url of this site is http://alexaboostup.com/ but before launching it you should sign up...my client want something like that but for another purpose...I already find out how to open a link in another tab with the combination of c# an javascript however I have no idea how to close it – Sara Nikta Yousefi Oct 16 '14 at 22:41
-
@sara.y 100+ likes on FB. wouldn't say the site is famous, but might take a look at it later – Steve Oct 16 '14 at 22:52
-
What is the difference between that site and http://alexa.netshahr.com/ ? Do they have same jobs ? – Omid Kosari Jan 15 '15 at 08:29
I Think you Should use This :
var win;
$('#click').click(function() {
win = window.open('http://www.ehowtutorials.net', "something");
});
$('#Cclick').click(function() {
win.close();
});
function closeit() {
win.close();
}
- 12,285
- 11
- 58
- 45