开发者

Problems opening/closing a 'popped-up' window

开发者 https://www.devze.com 2022-12-13 23:19 出处:网络
I have a web app that launches from an online portal into a new browser window. Everything works out fine until I try to open a new window or close the window from the webapp. Everything works fine wh

I have a web app that launches from an online portal into a new browser window. Everything works out fine until I try to open a new window or close the window from the webapp. Everything works fine when I test it locally though.

From what I've understood so far, after some Googling, it seems that this is because you can't call client side code from server side, for security reasons. I'll post what I've tried to implement for the Open and Close functions below:

Open

String javaScript = "window.open('http://www.goog开发者_如何学Pythonle.com', null, 'height=555,width=760,
                                  status=yes,toolbar=no,menubar=no,
                                  location=no, scrollbars=yes');";

ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", javaScript, true);

Close

<a href="javascript:window.open('','_parent','');window.close();">Close Window</a>

as well as a simple

<a href="JavaScript:window.close()">Close</a>

in the HTML itself.

I know this question has been asked countless times before, but I've gone diving in the forums and Google and still can't find something that works, so I appreciate any help in this.

For now I'm suspecting that the problem lies in trying to call a pop-up window from another pop-up window for the Open function, while for the Close function, the server does not recognize the handle of the window that opened the pop-up.

Edit:

My apologies to everyone if I didn't make myself clear enough. I'll try to explain in more detail here.

Basically, in my web application, I have these 2 buttons, Open and Close.

The Open button will open up a link to another website in a brand new browser window.

The Close button will close the browser window that the application is residing in.

When I tested it out locally, i.e. localhost:xxxxx/Game.aspx, everything works fine. However, the app currently does not reside in a separate pop-up window called from another browser window

On the test server, the application is launched into a new browser window from an online game repository portal.

So just to re-illustrate the way I want it to work:

1. User logs into game repository page

2. User chooses a game to play

3. Game pops up in a new browser window

4. When user clicks on Open button, another new window pops up to a specific website(Does not work)

5. When user clicks on Close button, the game window closes itself(Does not work)

I hope this makes the issue clearer. Thanks.


I'm not totally getting what you are trying to say, but hopefully i understand enough. You need to call Close() on the window object that you opened. So change your javascript to this:

string js = "var myWindow = window.open('http://www.google.com', null, 'height=555,width=760,
                              status=yes,toolbar=no,menubar=no,
                              location=no, scrollbars=yes');";

ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", js, true);

and then in your page:

<a href="JavaScript: if (typeof(myWindow) != 'undefined' && myWindow != null) myWindow.close()">Close</a>

If it helps then check this MSDN article.


Associate the popup windows to a variable which can in turn help you control the window which you will be closing, hence:


// Define the variable for the popup window
// You can use this to check if the window was indeed opened, before attempting to close
var myOpenedWindow = '';

// Open Window
myOpenedWindow = window.open('http://www.google.com', Null, 'height=555,width=760, status=yes,toolbar=no,menubar=no, location=no, scrollbars=yes');

// Close Window Function
function closeMyWindow() {
    // Check for window instance being open
    if (myOpenedWindow != '') {
        // Close the window
        myOpenedWindow.close();
        // Clear the variable
        myOpenedWindow = '';
    }
}

// Close Window [DIRECT]
closeMyWindow();

/* OR */

// Close Window [FROM POPPED WINDOW]
window.opener.closeMyWindow();

There's plenty of ways to control your popped windows, and hopefully with these examples you can better understand how to control them.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号