Force reload of HTMLViewer (2012r2)

I’m not sure of the changes to the HTMLViewer since 2012r2, so bear with me.

In 2012r2 how does one force the HTMLViewer to refresh?

I have a url where the page is essentially an image and when I urn the app, it works, showing the page. But when the image on the server changes, the app does not show the new image, seemingly holding cache somewhere. I could set some string with random characters after a “?” so the HTMLViewer thinks it’s grabbing a new url, but that doesn’t seem like the best way to go about it. The idea of chace is nice so the latest image loaded still shows when the app is not connected to the Internet, but I don’t want it blank if it’s not connected and searing for the “new random” url…

What is the proper way to do it?

I’m targeting OSX, but will want the same for Windows, too, if the code is different.

HTMLViever1.ExecuteJavaScript("location.reload();")

Should do it just fine.

So far, no good. I put this into the window.open event:

HTMLViewerLogo.LoadURL("http://example.com/try/logo.html") HTMLViewerLogo.ExecuteJavaScript("location.reload();")

After finding this reference: http://www.w3schools.com/jsref/met_loc_reload.asp I tried

HTMLViewerLogo.LoadURL("http://example.com/try/logo.html") HTMLViewerLogo.ExecuteJavaScript("location.reload(true);")

but still I have the old image…Am i putting th code in the wrong place?

just to check, if I do use the url http://example.com/try/logo.html?somethingrandom the correct image shows.

[quote=192483:@Dave Kondris]So far, no good. I put this into the window.open event:

HTMLViewerLogo.LoadURL("http://example.com/try/logo.html") HTMLViewerLogo.ExecuteJavaScript("location.reload();")

After finding this reference: http://www.w3schools.com/jsref/met_loc_reload.asp I tried

HTMLViewerLogo.LoadURL("http://example.com/try/logo.html") HTMLViewerLogo.ExecuteJavaScript("location.reload(true);")

but still I have the old image…Am i putting th code in the wrong place?

just to check, if I do use the url http://example.com/try/logo.html?somethingrandom the correct image shows.[/quote]

Browsers may have a rather aggressive caching policy. Worse, they may behave differently. The parameter solution though not conventional seems a good workaround.

I quickly looked around for solutions but nothing quite conclusive has popped up.

Yeah, I guess if I use the parameters, and check first to see if there is an internet connection, that should work.

Easiest way is to create a sub-class of the HTMLViewer, set the renderer to WebKit and then add the following pieces…

METHODS

[code]Sub ReloadFromOrigin()
#if TargetCocoa
Declare Sub reload Lib “Cocoa” Selector “reloadFromOrigin:” (id As Integer, sender As Integer)
reload me.Handle, me.Handle

#elseif TargetLinux
webkit_web_view_reload_bypass_cache(gtk_bin_get_child(me.Handle))

#elseif TargetWin32
DIM t_memoryblock As MemoryBlock = Ptr(me.Handle)
DIM t_reload_delegate As NEW CefReloadIgnoreCache(t_memoryblock.Ptr(48))
t_reload_delegate.Invoke(t_memoryblock)
#endif
End Sub[/code]

DELEGATES

Private Delegate Sub CefReloadIgnoreCache(p_pointer As Ptr)

EXTERNAL METHODS

Private Soft Declare Function gtk_bin_get_child Lib "libgtk" (bin As Integer) As Integer
Private Soft Declare Sub webkit_web_view_reload_bypass_cache Lib "libwebkitgtk-1.0.so" (web_view As Integer)

I haven’t played with Windows and Linux Xojo after I coded this the first time (thanks to Christian for the Windows code) so hopefully everything is still working properly on those platforms…

Ooh, I will try this. Thanks.

You could toss it in to a module as well, just need to modify the method to be an Extends for the viewer…

This is a bit confusing as I’ve never done anything like it before.

  1. I seem to have got the Privat Soft Declare SUB roght, but how do I make one so in place of SUB there is FUNCTION?

  2. Extends…I’m trying to go the route of your last comment and made a module called “SkipCache”. I’m guessing that I call something like this in the HTMLviewer:

me.LoadURL("http://example.com/a/flogo.html") skipcache.ReloadFromOrigin

I’ve got the method set to global, but I’m not getting autfill after the “.” So, I must be doing something wrong?

Here is a screen shot of my module, for the sake of simplicity:

If it’s in a module and you are extending the HTMLViewer, just do me.ReloadFromOrigin
Also, you will need to change the “me.Handle” in the Linux code to “h.Handle”

Ok, this is rather frustrating. I have just tried to trim it all down to the bare essentials. Have a single window with two HTMLViewers.

HTMLViewer1 and HTMLViewer2

In the open events I put:

me.LoadURL("http://www.example.com/a/minisite.html") me.ReloadFromOrigin

me.LoadURL("http://www.example.com/a/logo.html") me.ReloadFromOrigin

Here are screeshots of my module:

I’m using RealStudio 2012r2.1 on MPB with Mavericks. YET, when I make this new project, the HTMLViewers show PREVIOUS html pages, not even the curent ones. So, somewhere the webkit is saving cache and not even letting a brand new project present the new pages. When I use my regular browser (Firefox) the pages of course show correctly.

Any help on identifying where I’ve gone wrong is appreciated.

WORKAROUND - I’ve decided to use an HTTPSocket and just download the webpages I need and write html files in the project bundle and then use the htmlviewer to show those.

This seems to be working. I can change the html on my server and the changes are reflected immediately in my app now. That’s the result I’ve been looking for.

If anyone believes this may be a bad idea, please feel free to let me know…

Also, I probably need to check that the download occurs and if not, write some other file to show in the viewer.

OTOH, if I’ve a simple error I can fix above in the original thread, I might go back to trying that.

[quote=193576:@Dave Kondris]WORKAROUND - I’ve decided to use an HTTPSocket and just download the webpages I need and write html files in the project bundle and then use the htmlviewer to show those.

This seems to be working. I can change the html on my server and the changes are reflected immediately in my app now. That’s the result I’ve been looking for.

If anyone believes this may be a bad idea, please feel free to let me know…

Also, I probably need to check that the download occurs and if not, write some other file to show in the viewer.

OTOH, if I’ve a simple error I can fix above in the original thread, I might go back to trying that.[/quote]

This won’t work for pages with relative path to resources. For instance, if I have a page at http://Fontmenu.com and a picture with a path such as …/pics/normographe.gif when you download your page to your disk the HTML will look for the picture next to it within a pic folder which it will of course not find, so you will end up with no picture. It will definitely not work with dynamic pages such as a Xojo web app since they depend on the JavaScript framework.

Your workaround of simply adding some random parameter is a much simpler, and much better idea IMHO. Complex is not necessarily better.

Hi Michel,

thanks for the comment. I may then go with the random parameter. I did realize the relative paths would be a problem, but it’s not problem to write the html with full http://… paths.

But I agree the random parameter is probably simpler. I’d actually almost forgotton about that idea, thanks for reminding me! )

EDIT: I recall my reservation with the parameter - ironically, it makes the actually, useful cache useless. Without the parameters, the viewer will load the cache, but with the random parameters, if there is no internet connection, the app will get a blank page. In that case, I’d still need to check for a connection and have some sort of local html file to show anyhow.

Obviously, what I want is an always-up-to-date cache…

Maybe shao sean will come back and clear it all up for me))

from your screenshots it looks like you are using REALbasic still, so not too certain how much is different in there as these pieces of code were written in Xojo, so the Windows DLL might have the functions in a different memory location… Mac OS X and Linux should still be the same though, as they are just straight calls to the system APIs…