When I use the back button on Firefox to reach a previously visited page, scripts on that page won’t run again.
Is there any fix/workaround to have the scripts execute again when viewing the page the second time?
Please note that I have tested the same pages on Google Chrome and Internet Explorer and they work as intended.
Here are the files and the steps I used to test the problem:
(navigate to 0.html, click to get to 1.html, back button)
0.html
JavaScript
x
8
1
<html><body>
2
<script>
3
window.onload = function() { alert('window.onload alert'); };
4
alert('inline alert');
5
</script>
6
<a href="1.html">Click Me!</a>
7
</body></html>
8
1.html
JavaScript
1
4
1
<html><body>
2
<p>Go BACK!</p>
3
</body></html>
4
Advertisement
Answer
Set an empty function to be called on window.onunload:
JavaScript
1
2
1
window.onunload = function(){};
2
e.g.
JavaScript
1
9
1
<html><body>
2
<script type="text/javascript">
3
window.onload = function() { alert('window.onload alert'); };
4
window.onunload = function(){};
5
alert('inline alert');
6
</script>
7
<a href="1.html">Click Me!</a>
8
</body></html>
9
Source: http://www.firefoxanswer.com/firefox/672-firefoxanswer.html (Archived Version)