Closing unneeded pages

I am trying to do some cleanup by closing pages that were opened but aren’t needed. I think some of the pages might be interfering with overall speed of app.

Dim pc as integer = Session.PageCount
Dim pi as integer = 0
MsgBox "Page count before closing: "+Str(pc)
for pi = 0 to pc-2
   Session.PageAtIndex(0).Close
next
pc = Session.PageCount
MsgBox "Page count after closing: "+Str(pc)

I get an error (I believe on the last two lines). Anything wrong here?

what is error?

you may want to leave current page unclosed.

Yeah, you need to iterate that pi backwards.

I opened page with highest index and that was current page. Not what I thought was true; I must have been doing something wrong. Anyway, you are probably right. I’ll try it tomorrow.

for pi = pc-2 to 0 step -1

Is PageAtIndex(0) always currently opened page?

I use -2 because index is 0-based plus I want to leave current page open.

the problem is that if you have 5 pages, you’re looping from 0 to 4. When you get to 3, PageAtIndex(3) will be nil.

I just tested and it looks like the highest index page is the page which is currently in front. I wouldn’t personally rely on this in any case. It’s one of those things that could change if Xojo changes an algorithm and then your app breaks for no apparent reason.

Here is sample code to close all open pages except the page that is currently in front.

  Dim p As WebPage
  Dim i, c As Integer
  c = Session.PageCount - 1
  
  For i = c To 0 Step -1
    p = Session.PageAtIndex(i)
    If p <> Session.CurrentPage Then
      p.Close
    End If
  Next

When I use your code I don’t get an error so thanks.

I didn’t know about the Session.CurrentPage command either. That helps.

I am still a little unclear about how the index changes after each p.close but I’ll figure it out.