English: Performance Still DuPage Opera Theatr...

English: Performance Still DuPage Opera Theatre Carmen (Photo credit: Wikipedia)

Adding an (even empty) unload event to your page ensures that most browsers automatically re-request the page when a visitor returns via the back-button. Unfortunately, Opera will still “reload” the page from its own cache. Try Demo without Fix (opens in new tab).

When might you want to reload from server?  Example, you use the unload event to “kill” the server session on page exit (see why sessions should be closed soonest). If a user later “back-buttons” to your page, most browsers will re-request the page (starting a new session). Opera loads the page from cache – result no server session and your page’s form will not work (the user would have to know to manually refresh the page).

From Googling, and testing, the addition of cache control headers etc don’t force Opera to reload from server either.

The Javascript solution:

I could find plenty of how do I stop Opera from caching questions, but no solutions that worked. So I wrote some Javascript to do the job; it makes use window.name “variable” which is persitent and not reset or destroyed on exit to another page.

It worked fine in my tests; compare the Before Demo and After Fix Demos (both open in new tabs). However, I do not guarantee the code below is complete, or covers all possibilities and circumstances.

In the Head section above any other javascript, add:

<script type="text/javascript">
if( typeof(opera) != undefined ) { // only do for Opera
  if (window.name != "") { // will be '' if page not prev loaded
    alert('Reloading Page from Server'); // FOR TESTING ONLY
    window.name = ""; // reset to prevent infinite loop

Now add some code to name the window, in a Javascript block that wont be executed during “window load”. e.g.
window.name = new Date().getTime(); // makes name unique

Where should I place this line:

ideally in one of your event handler functions that will always be executed* in circumstances where you need it. I use the onclick or unload event to kill the server session when a visitor exits via a link . So I can simply add the above line to this function.

for all types of navigation out and back (e.g. returning after navigation away by bookmark ) add the line to one of your  <script> blocks. Adding it in the wrong place may cause infinite reloads. For performance I place most Javascript just above the closing body tag; and including the above line in a <script> block there worked fine in my tests.

* Opera only fires the unload event on page exit via a link.

Notes on the Code:

I wrote the code only because I couldn’t find a solution online, hopefully it will be of use to others.  I am NOT a Javascript expert or guru; so the code may not be elegant, cover all cases, or be the best way of doing it.

It works for me with Opera (12.14) using pages with <!DOCTYPE html>; and I’ve no reason to believe it wont work for other versions of Opera and othe document types, but obviously I can’t guaranttee it will work in all circumstances.

Feel free to use the comments to let me know whether or not it works with other versions of Opera; or to critique the code, or to suggest alternative  solutions.

Author: Andy W+

Enhanced by Zemanta