Autodidact
Autodidact

Reputation: 768

The Safari Back Button Problem

I do some minor programming and web work for a local community college. Work that includes maintaining a very large and soul destroying website that consists of a hodge podge of VBScript, javascript, Dreamweaver generated cruft and a collection of add-ons that various conmen have convinced them to buy over the years.

A few days ago I got a call "The website is locking up for people using Safari!" Okay, step one download Safari(v3.1.2), step two surf to the site. Everything appears to work fine.

Long story short I finally isolated the problem and it relates to Safari's back button. The website uses a fancy-pants javascript menu that works in every browser I've tried including Safari, the first time around. But in Safari if you follow a link off the page and then hit the back button the menu no longer works.

I made a pared down webpage to illustrate the principle.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Safari Back Button Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body onload="alert('Hello');">
<a href="http://www.codinghorror.com">Coding Horror</a>
</body>
</html>

Load the page and you see the alert box. Then follow the link off the page and hit the back button. In IE and Firefox you see the alert box again, in Safari you do not.

After a vigorous googling I've discovered others with similar problems but no really satisfactory answers. So my question is how can I make my pages work the same way in Safari after the user hits the back button as they do in other browsers?

If this is a stupid question please be gentle, javascript is somewhat new to me.

Upvotes: 50

Views: 42561

Answers (12)

In my Angular application I used this condition to fix the problem. First I added onunload="" event handle to index.html lilke below

<body class="mat-typography" onunload="">
    <app-root></app-root>
</body>

then in my component ngOnInit method I added that code like below

  public ngOnInit(): void {
    window.onpageshow = (event) => {
        if (event.persisted) {
            document.body.style.display = "none";
            location.reload();
        }
    };
    ...
  }

Upvotes: 0

Alok Kumar Kasgar
Alok Kumar Kasgar

Reputation: 109

Try this
if(performance.navigation.type == 2) {
alert("Hello");
}

Upvotes: 0

ztech
ztech

Reputation: 560

$(window).bind("pageshow", function(event) {
  if (event.originalEvent.persisted) {
    window.location.reload() 
  }
});

Answered more recently here by mshah.

Upvotes: 4

bluesmoon
bluesmoon

Reputation: 4340

Please do not follow any of the advice that tells you to ignore the cache. Pages are cached for a reason -- to improve user experience. The methods you're using will make user experience worse, so unless you hate your users, don't do that.

The correct solution for Safari (Desktop and iOS) is to use the pageshow event instead of the onload event (See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching for what these are).

The pageshow event will fire at the same time you expect the onload event to fire, but it will also work when pages are served via the cache. This appears to be what you want anyway.

Upvotes: 9

ArnaudBB
ArnaudBB

Reputation: 9

Had the same problem on iPad.

Not that beautiful but it works :). How it works.

I realised that on iPad Safari, the page was not reloaded when the back button was pressed. I put a counter every second on the page and I save the current timestamp.

When the page is loaded the counter and time are synchronized. On back button, counter continue where it stopped and there is a gap between timestamp and counter. If the gap is grater than 500ms, force reload the page.

In the file action.js

var showLoadingBoxSetIntervalVar;
var showLoadingBoxCount = 0;
var showLoadingBoxLoadedTimestamp = 0

function showLoadingBox(text) {

    var showLoadingBoxSetIntervalVar=self.setInterval(function(){showLoadingBoxIpadRelaod()},1000);
    showLoadingBoxCount = 0
    showLoadingBoxLoadedTimestamp = new Date().getTime();

    //Here load the spinner

}

function showLoadingBoxIpadRelaod()
{
    //Calculate difference between now and page loaded time minus threshold 500ms
    var diffTime = ( (new Date().getTime()) - showLoadingBoxLoadedTimestamp - 500)/1000;

    showLoadingBoxCount = showLoadingBoxCount + 1;
    var isiPad = navigator.userAgent.match(/iPad/i) != null;

    if(diffTime > showLoadingBoxCount && isiPad){
        location.reload();
    }
}

Upvotes: 0

Gary Hayes
Gary Hayes

Reputation: 1788

Here is a good solution for Mobile Safari:

/*! Reloads page on every visit */
function Reload() {
    try {
        var headElement = document.getElementsByTagName("head")[0];
        if (headElement && headElement.innerHTML)
            headElement.innerHTML += " ";
        } catch (e) {}
    }

    /*! Reloads on every visit in mobile safari */
    if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) {
        window.onpageshow = function(evt) {
            if (evt.persisted) {
                document.body.style.display = "none";
                location.reload();
            }
        };
    }

(source)

I modified it to my needs, but as is it is okay. (annoying white screen on refresh if you dont modify it).

Upvotes: 8

Manuel Pardo
Manuel Pardo

Reputation: 127

Just put this in body tag <body onunload="">

this will force safari, chrome, FF reload every time you hit Back button

Upvotes: 11

brunam
brunam

Reputation: 835

I know this thread is a year old, but I just fixed an identical Safari-only problem using ProjectSeven's Safari backbutton fix. Works like a charm.

http://www.projectseven.com/extensions/info/safaribbfix/index.htm

Upvotes: 1

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

Stefan's iframe solution works, but if that's not elegant enough, I find the following JavaScript also solves it:

window.onunload = function(){};

That is, if your menu is JavaScript, then you might prefer to solve this issue with JavaScript too.

The unload event handler definition idea came from this Firefox 1.5 article: https://developer.mozilla.org/en/Using_Firefox_1.5_caching.

Upvotes: 19

Stefan Ladwig
Stefan Ladwig

Reputation: 589

An iframe solves the problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Safari Back Button Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body onload="alert('Hello');">
<a href="http://www.codinghorror.com">Coding Horror</a>
<iframe style="height:0px;width:0px;visibility:hidden" src="about:blank">
this prevents back forward cache
</iframe>
</body>
</html>

more details

Upvotes: 7

Stacey Richards
Stacey Richards

Reputation: 6596

I've noticed something very similar. I think it is because Firefox and IE, when going back, are retrieving the page from the server again and Safari is not. Have you tried adding a page expiry/no cache header? I was going to look into it when I discovered the behaviour but haven't had time yet.

Upvotes: 3

Teifion
Teifion

Reputation: 111109

I have no idea what's causing the problem but I know who might be able to help you. Safari is built on Webkit and short of Apple (who are not so community minded) the Webkit team might know what the issue is.

It's not a stupid question at all.

Upvotes: 3

Related Questions