[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

window.onload not working when using Safari's back button

Tuxedo

5/10/2016 8:54:00 PM

Hello,

If I have a window.onload event called from in a script or in the body tag,
for example:

window.onload=alert('hello');
or
<body onload="alert('hello')">

.... and after having navigated away from the page via for example a link
and if thereafter using the browser's back button, the onload event will
occur again when returning to the original page in FF and in other
browsers, but not in Safari for some reason - why not?

I tested on an Ipad. Presumably Safari on a desktop OSX behaves the same as
on the Ipad. But Chrome, which is also utilising WebKit, behaves like FF in
this respect, so the difference appears to be Safari specific.

Is it possible to detect if a page is returned to via the back button in
Safari?

Many thanks for any advise.

Tuxedo
2 Answers

JJ

5/11/2016 7:02:00 AM

0

On Tue, 10 May 2016 22:53:39 +0200, Tuxedo wrote:
> Hello,
>
> If I have a window.onload event called from in a script or in the body tag,
> for example:
>
> window.onload=alert('hello');
> or
> <body onload="alert('hello')">
>
> .... and after having navigated away from the page via for example a link
> and if thereafter using the browser's back button, the onload event will
> occur again when returning to the original page in FF and in other
> browsers, but not in Safari for some reason - why not?

The page is probably restored (not reloaded) from the browser's memory
cache. A cache which include not just the web page resources, but also the
DOM state. An optimized browser would restore a page from cache if possible,
rather than simply reload it.

> I tested on an Ipad. Presumably Safari on a desktop OSX behaves the same as
> on the Ipad. But Chrome, which is also utilising WebKit, behaves like FF in
> this respect, so the difference appears to be Safari specific.
>
> Is it possible to detect if a page is returned to via the back button in
> Safari?

No. There's no event for browser's navigational actions except browser's own
non standard events which are only accessible from with a browser extension.

Tuxedo

5/11/2016 7:40:00 PM

0

JJ wrote:

> On Tue, 10 May 2016 22:53:39 +0200, Tuxedo wrote:
> > Hello,
> >
> > If I have a window.onload event called from in a script or in the body
> > tag, for example:
> >
> > window.onload=alert('hello');
> > or
> > <body onload="alert('hello')">
> >
> > .... and after having navigated away from the page via for example a
> > link and if thereafter using the browser's back button, the onload event
> > will occur again when returning to the original page in FF and in other
> > browsers, but not in Safari for some reason - why not?
>
> The page is probably restored (not reloaded) from the browser's memory
> cache. A cache which include not just the web page resources, but also the
> DOM state. An optimized browser would restore a page from cache if
> possible, rather than simply reload it.

Thanks for clarifying this. It is indeed what happens with Safari and maybe
other browsers but not in FF and the Chrome browser.

> > I tested on an Ipad. Presumably Safari on a desktop OSX behaves the same
> > as on the Ipad. But Chrome, which is also utilising WebKit, behaves like
> > FF in this respect, so the difference appears to be Safari specific.
> >
> > Is it possible to detect if a page is returned to via the back button in
> > Safari?
>
> No. There's no event for browser's navigational actions except browser's
> own non standard events which are only accessible from with a browser
> extension.

The issue was that the user can modify elements on the page temporarily
which should not stay in their modified states after having left and
returned to the same page. To work around the different browsers I can
instead detect certain display states by inspecting various style elements
using for example setInterval and thereafter restore the wanted defaults
after the page was once unloaded and if the back button happens to
thereafter be used.

Tuxedo