Close Open Web Pages

Is there a way to detect and close any open web pages or dialogs in a web app? I don’t want to leave big, heavy pages open and taking up resources if I can help it.

For example, users are able to use a shared container control for navigation, but I’d like to close any pages they may have opened as they move to another page. I’m not sure how the navigation control could tell which dialog or page called it so that it would know which one to close as it moved to a new page.

I thought maybe I could just issue a command to close all other windows, but I’ve discovered that if you call

Webpage1.close

and the page wasn’t open to begin with, it will first open, run any code in the Open and Shown events, and then close (or issue an error).

Any thoughts on how I should go about it? I’ve tried code using WebPage1.Lostfocus, but it doesn’t seem to do anything helpful.

You can iterate through all open pages in the Session using WebSession.PageCount http://documentation.xojo.com/index.php/WebSession.PageCount and WebSession.PageAtIndex http://documentation.xojo.com/index.php/WebSession.PageAtIndex

FWIW, I recommend turning off ImplicitInstance. That way you can’t call webPage1.close. The only way you can open a page is to create a new instance of it:

dim pg as new WebPage1 pg.show

What is the advantage of doing it that way?

You’re a genius, Bob! Thanks! I just ran the code below and it seems to do the job

dim x as integer for x = 0 to Session.pagecount - 1 if Session.PageAtIndex(x) <> Session.PagewithName("mainPage",true) then Session.PageAtIndex(x).close end if next x

[quote=209132:@Peter Stallo]You’re a genius, Bob! Thanks! I just ran the code below and it seems to do the job

dim x as integer for x = 0 to Session.pagecount - 1 if Session.PageAtIndex(x) <> Session.PagewithName("mainPage",true) then Session.PageAtIndex(x).close end if next x[/quote]

Closing WebPages is fine, but if for any reason you need to display them again, it will take longer.

Implicit Instance means that you can do some silly stuff like

Webpage1.close

because you automatically open an instance of that page and then close it. Accessing ANY property on WebPage1 will open it because that’s what implicit instance means. Turning implicit instance off means that you’ll never have that problem.

I’ve found over the years that having ImplicitInstance = True can lead to some very subtle errors in Window (desktop) and Page (web) handling. Turn implicit instance off on all windows/pages and you’ll often find where those problems are occurring. If I had my way, Xojo would allow us to turn that off as a default.