Solved

Window.close of javascript is not working

Posted on 2013-01-16
17
478 Views
Last Modified: 2013-01-31
I have two asp.net applications.

One app is referring a page in other app.
On that page i have a cancel button on which i am calling window.close() JavaScript function.

But when the app that refers to the page in other app, this cancel button is not working.

Please guide,
Thanks
0
Comment
Question by:Aspnet_9
  • 9
  • 7
17 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782559
can u post the relevant code in the page which you try to close?
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38782565
try one of the following:
self.close(),
window.parent.close();
var win = window.open("","_self");
win.close();
0
 

Author Comment

by:Aspnet_9
ID: 38782591
This is function being called on page with cancel button.
        function CancelClick() {
            window.self.close();
        }

Code used to call the above page

            this.winNotes.AutoLoad.Url = System.Web.Configuration.WebConfigurationManager.AppSettings["Te"] + "/views/Note/Add.aspx;
            this.winNotes.LoadContent();
            this.winNotes.Show();
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782596
did u try what i've suggested?
which browser you use?
0
 

Author Comment

by:Aspnet_9
ID: 38782621
I have tried all the above options mentioned by you. None is working.

I am using IE,Firefox
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782631
when running in FF do you get any errors?
0
 

Author Comment

by:Aspnet_9
ID: 38782640
No, its not.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782652
In FF, window.close() will close only if opened via window.open:

window.close
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 42

Expert Comment

by:sedgwick
ID: 38782670
try the following:

function winClose()
{
var win=window.open("","_self");
win.close();
}

Open in new window

or
function winClose()
{
window.top.opener=null;
window.close();
}

Open in new window

or
function closeWindow()
{
window.open('','_parent',''); 
window.close();
}

Open in new window


if not set your firefox browser:

1.

input "about:config " to your firefox address bar and enter;

2.

make sure your "dom.allow_scripts_to_close_windows" is true


fromMozilla Firefox JavaScript Window.Close() Not Working
0
 

Author Comment

by:Aspnet_9
ID: 38782672
I cannot open the window using window.open as it is being called programatically from other app.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782848
i understand that you can't i'm just showing you FF restrictions.
did u try the other suggestions?
0
 

Author Comment

by:Aspnet_9
ID: 38782875
Unfortunately they are not working.

is there any way in can close the window using c# code in code behind?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782890
you can try the following:

ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "close", "window.close();", True);

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38782899
if its not working try:
protected void Page_Load(object sender, EventArgs e)    
{
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "close", "window.onunload = CloseWindow();");
}

Open in new window


and in your aspx page add closePage function:
function closePage() {
    window.close();
}

Open in new window

0
 

Author Comment

by:Aspnet_9
ID: 38782939
this is again calling the javascript through codebehind,
I dont think its going to work
0
 
LVL 6

Expert Comment

by:esolve
ID: 38786061
Not sure if I understand your scenario but if you would like to close a page from a button click I usually do the following. Add an image to your page which will prevent the page from doing a postback. We're using nice images which looks like a button.

Then in the code behind of the same page you add the following

imgCancel.Attributes.Add("onclick", "window.close();");

You can also force a refresh on the parent page causing it to reload.
0
 

Author Closing Comment

by:Aspnet_9
ID: 38840926
ok
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now