Store Visiting Pages In Array
In a browser, a user navigates from one URL to another. How do I store all the URLs visited in an array variable? Moreover there shouldn't be duplicates in the array. For e.g. if a user visits, say, www.google.com, it should be stored in the array. If he visits www.google.com again during the same session (or until the user closes the browser window), it shouldn't get added to the array again. Also each open browser window should have it's own array variable. For e.g. if a user visits www.site1.com & www.site2.com in the 1st browser window, the array should hold www.site1.com & www.site2.com. If the user opens another browser window & visits www.abcd.com & www.efgh.com, then the array in the 2nd browser window should hold www.abcd.com & www.efgh.com.
How do I accomplish these tasks?
Re: Store Visiting Pages In Array
Try something like this - it is presumed that your webbrosers are control array (I used textbox lfor simplicity):
Code:
Option Explicit
Private Type URL
URL() As String
End Type
Dim arUrls() As URL
Private Sub Form_Load()
Dim i%
ReDim Preserve arUrls(Text1.UBound)
For i = 0 To UBound(arUrls)
ReDim arUrls(i).URL(0)
Next i
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
'add url to array
'NOTE: you may need to scan current array for dups before inserting new item;
' don't scan it if you want to keep entire history
If KeyAscii = 13 Then
arUrls(Index).URL(UBound(arUrls(Index).URL)) = Text1(Index).Text
ReDim Preserve arUrls(Index).URL(UBound(arUrls(Index).URL) + 1)
End If
End Sub
'run this to test what's in the array
Private Sub Command1_Click()
Dim i%
For i = 0 To UBound(arUrls(0).URL) 'I'm only checking main array's first element
Debug.Print arUrls(0).URL(i)
Next i
End Sub
Re: Store Visiting Pages In Array
But the browsers are not control array. In fact, there is just one Form with the WebBrowser control. Since the WebBrowser control is nothing but IE, users can open a link by right-clicking it & selecting Open in New Window. So it is not necessary that more than one browser window will be open at a given time. It depends upon users. That's what I meant by "each browser window".
It's not a control array.
Re: Store Visiting Pages In Array
You can still use that array but you will need to add some unique identifier to URL type and also make it public.
Re: Store Visiting Pages In Array
Code:
Option Explicit
Private Type URLRecord
Browser As String
url As String
End Type
Private Sub Form_Load()
Dim ur() As URLRecord
ReDim ur(0)
ur(UBound(ur)).Browser = "blah"
ur(UBound(ur)).url = "www.vfforums.com"
' for each new entry
ReDim Preserve ur(UBound(ur) + 1)
End Sub
Re: Store Visiting Pages In Array
Martin, could you please explain what you have done? Sorry to say I couldn't exactly understand it. How do I loop through the array?
Re: Store Visiting Pages In Array
Code:
Dim ur() As URLRecord
Dim lngIndex As Long
Dim strBrowser As String
Dim strURL As String
Dim x As Integer
ReDim ur(0)
Do
strBrowser = InputBox("Browser name")
strURL = InputBox("URL")
For lngIndex = 0 To UBound(ur)
If strBrowser = ur(lngIndex).Browser And strURL = ur(lngIndex).url Then
Exit For
Else
ur(UBound(ur)).Browser = strBrowser
ur(UBound(ur)).url = strURL
ReDim Preserve ur(UBound(ur) + 1)
End If
Next
' for testing
x = x + 1
If x = 3 Then Exit Do
Loop
ReDim Preserve ur(UBound(ur) - 1)
For lngIndex = 0 To UBound(ur)
Debug.Print ur(lngIndex).Browser & " - " & ur(lngIndex).url
Next
Re: Store Visiting Pages In Array
Thanks, Martin, for the code but I couldn't make it work. If I visit a URL, it gets added to the array...that's fine but if I visit that URL again, it again gets added to the array. Here are the changes I made:
Code:
Private Type URLRecord
PgURL As String
PgTitle As String
End Type
Private Sub wWeb_DownloadComplete()
Dim ur() As URLRecord
Dim lngIndex As Long
Dim strTitle As String
Dim strURL As String
Dim x As Integer
ReDim ur(0)
Do
strTitle = wWeb.LocationName
strURL = wWeb.LocationURL
For lngIndex = 0 To UBound(ur)
If strTitle = ur(lngIndex).PgTitle And strURL = ur(lngIndex).PgURL Then
Exit For
Else
ur(UBound(ur)).PgTitle = strTitle
ur(UBound(ur)).PgURL = strURL
ReDim Preserve ur(UBound(ur) + 1)
End If
Next
' for testing
x = x + 1
If x = 3 Then Exit Do
Loop
ReDim Preserve ur(UBound(ur) - 1)
For lngIndex = 0 To UBound(ur)
Debug.Print ur(lngIndex).PgTitle & " - " & ur(lngIndex).PgURL
Next
End Sub
Any idea where I could be going through?
Note that I have put your code in the DownloadComplete event of the WebBrowser control. Is that the correct place to put your code? I am not sure....
Also could you please explain me the logic behind your code? I couldn't exactly follow it....
Giving you lot of trouble, isn't it?
Re: Store Visiting Pages In Array
Code:
Private Sub wWeb_DownloadComplete()
Dim ur() As URLRecord 'move this and the line below that also is commented with "move"
'out of wWeb_DownloadComplete and put them someplace where they will
'only be done once
Dim lngIndex As Long
Dim strTitle As String
Dim strURL As String
Dim x As Integer
ReDim ur(0) 'move (see above)
Do ' You don't need this since I assume that you only want to add one entry
' every time wWeb_DownloadComplete
strTitle = wWeb.LocationName
strURL = wWeb.LocationURL
For lngIndex = 0 To UBound(ur)
If strTitle = ur(lngIndex).PgTitle And strURL = ur(lngIndex).PgURL Then
Exit For
Else
ur(UBound(ur)).PgTitle = strTitle
ur(UBound(ur)).PgURL = strURL
ReDim Preserve ur(UBound(ur) + 1)
End If
Next
' none of the next three lines are needed
x = x + 1
If x = 3 Then Exit Do
Loop
ReDim Preserve ur(UBound(ur) - 1)
'you don't need this either
For lngIndex = 0 To UBound(ur)
Debug.Print ur(lngIndex).PgTitle & " - " & ur(lngIndex).PgURL
Next
End Sub
Re: Store Visiting Pages In Array
Quote:
'move this and the line below that also is commented with "move" out of wWeb_DownloadComplete and put them someplace where they will only be done once
What do you mean by "some place"? Should those 2 lines be put in an event function of the WebBrowser control or can it put in any Form/other controls' event function?
Re: Store Visiting Pages In Array
Quote:
Originally Posted by arpan_de
... if I visit that URL again, it again gets added to the array...
In my opinion that's how it should work - look at IE history and you will see list of all the pages you visited and not only unique ones.
You may limit however number of entries in your array if you wish.
Re: Store Visiting Pages In Array
Quote:
Originally Posted by arpan_de
What do you mean by "some place"? Should those 2 lines be put in an event function of the WebBrowser control or can it put in any Form/other controls' event function?
I assume that you have a place in your app where you do various initialization type coding. Put the two lines there.