I want to close a popup window from an error page. The process is:
- S开发者_JAVA技巧ubmit a form
- At form submission, watch a popup window open
- If an error is thrown after form submission, then view the page (not the popup) redirect to an error page
At this time, the popup window should close.
Given the timing of the opening of the popup window and the submission of the form, there isn't an easy way to wait and see if an error is thrown and then decide whether or not to open a popup window. The specific error that I'm programming against is thrown after the opening of the popup window, so I believe the popup window should be closed from the error page. The steps are set in place, so I'm looking for a solution for the current architecture.
On the error page, I've tried calling a function from the same script file that includes the function responsible for opening the popup window. The related code is:
var PopupWnd = null;
function ShowPopup(){
PopupWnd = window.open('Popup.aspx','PopupWnd','');
}
function HidePopup(){
if(PopupWnd != null){
if(!PopupWnd.closed){
PopupWnd.close();
}
}
}
From the HidePopup()
function, shouldn't the PopupWnd
object be not equal to null if the ShowPopup()
function was executed earlier? Is it related to a scoping issue or timing of events? How do I make the PopupWnd
object value persist so that it's not equal to null when I want to close the popup window in the HidePopup()
function?
string script = "<script>HidePopup();</script>";
ClientScript.RegisterClientScriptBlock(GetType(), "UploadErrorScript", script);
For a sanity check, I entered in alerts to make sure that the HidePopup function is being called from my codebehind and the PopupWnd object is still set equal to null within this function.
You may have a collision in the names. You have a variable PopupWnd and the name of the window PopupWnd.
var PopupWnd = null; function
ShowPopup(){
PopupWnd = window.open('Popup.aspx','PopupWindow','');
}
function HidePopup(){
if(PopupWnd != null){
if(!PopupWnd.closed){
PopupWnd.close();
}
}
}
That should work as the WindowName no longer collides with the variable. I'd suggest checking out http://www.infimum.dk/HTML/JSwindows.html for a more indepth view on window manipulation using javascript.
In a case like this, I'd also suggest Line by Line testing and doing things such as throwing out an alert i.e. do an alert after if PopupWnd != Null to see if it even reaches that part. Hell, do an alert right at th ebeginning of the HidePopup function to see what the value of PopupWnd is...
精彩评论