2

I am using MVC 3 and I have a button that the user presses to either update some records or to not update some records. I am trying to close the tab after the user presses the No, thank you button. After doing some research all that I have been able to find on this subject is to close a window using jQuery or javascript. I was hoping that there is a way to close the window in the actionLink. I have included the button code below and feel free to ask any questions you might have as I will be checking back often. If you need any other code I would be happy to supply that as well. Thanks for your help!

<div class="message dialog" title="Changes Saved">
            You have successfully saved your changes.<br/>Would you like to update the other versions of this Project?
            <br />
            @Html.ActionLink("Yes, Update Projects", "UpdateProject", "Project", new { id = Model.IAMP_PK }, new { @class = "button2" })
            @Html.ActionLink("No, Thank you", null, null, new { @class = "button2" })
            </div>
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Goldentp
  • 187
  • 1
  • 7
  • 28

3 Answers3

4

You cannot close the browser from server-side. And I am not sure why you want that when client-side solution is pretty simple.

Change the button to:

@Html.ActionLink("No, Thank you", null, null, new { @class = "button2", id="close" })

Then do

$("#close").click(function() { window.close(); });
Mohayemin
  • 3,841
  • 4
  • 25
  • 54
2

javascript is answer as client-side code is only way to close the browser. You could attach a jquery onclick event to the button based on class=button2.

http://api.jquery.com/click/

$(".button2").click(function() { window.close(); });

How to close current tab in a browser window?

Community
  • 1
  • 1
Shawn
  • 1,871
  • 2
  • 21
  • 36
  • I can't use the class button2 in a jQuery onclick event because I use button2 throughout the code as a css holder so any class called button 2 will display a certain way. I just don't want the browser to close whenever a button2 class is pressed. Is there maybe another way other than basing it on the button2 class? – Goldentp Aug 14 '12 at 16:57
  • Yes, you use the id tag. Just give the button an id as you did a the class and use that. – Shawn Aug 14 '12 at 17:10
  • Thank you for your response as well, it says that you and Mohayemin answered at the same time and if I could I would give you both the answer because you are 100% correct but have an upvote with my thanks – Goldentp Aug 14 '12 at 18:26
1

It is very simple to close window or tab which is related to our current project.

After Create a Button with onclick function name(closeWindow) use the below code.

function closeWindow() {
    alert("Your Tab is closing.....");
    //window.open('', '_self', '');
    self.close();
}
tripleee
  • 175,061
  • 34
  • 275
  • 318