Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi am opening a MVC view as a popup using the below code..

window.open("../Employee/Report?Id=" + '<%= (string) Model.clsobj[0].ID %>' + "&Name=" + '<%= (string) Model.clsobj[0].Name %>' , 'MyReport', 'left=250,top=80,width=800,height=520,toolbar=1,resizable=0');

If opened i have to close this window on click of logout button.(logout button is on master page) Please guide ..
Posted
Comments
Sergey Alexandrovich Kryukov 4-Dec-15 3:11am    
In general case, it's impossible to close a browser window. (Note that it can also appear as a tab, you never know.) In a special case, it can be closed, but it's better not to rely on it.
—SA

1 solution

Please see my comment to the question. First of all, I would strongly advise to review your UI design and avoid such things. The problem is not even closing. More basic problem is window.open. This is called "popup" and has very bad reputation. Not only it is too intrusive to be convenient enough for the users, but many user fight against popups with popup blocking software, most typically in the form of browser plug-ins. Your whole window may not show up at all.

After this warning, I'll give you the formal answer, but it won't work in a number of cases. Here is the recipe:
JavaScript
var popup = window.open( //...

//...

popup.close();

Also, window.close() closes current window: https://developer.mozilla.org/en-US/docs/Web/API/Window/close[^].

But the current-window approach may not work in some cases. Say, it won't work if your window is actually a tab. JavaScript cannot "see" the difference, but it won't just work. First approach based on the return from open should work.

I already suggested: don't do it. One reasonable alternative would be so called modal popup (this name is doubly confusing; in fact, this is some emulation of modal-like behavior on the same page; but it solves nearly all problems). For further detail, please read my last article, where I explain it all in detail, and, but they way, provide yet another implementation: Modal Popup From Scratch[^].

But you don't have to use my implementation: there is jQuery Dialog Widget and many (really many) 3rd-party plug-ins and other implementations.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900