Skip to content

Commit 965ddd3

Browse files
committed
Added popstate workaround
1 parent 5aa4443 commit 965ddd3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

frontend/static/js/pastee.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,31 @@ function init() {
2323
noWrapMode();
2424
noLinkifyMode();
2525
}
26+
// Call init() on page load.
27+
$(init);
2628

2729

2830
// Re-initialize page state if the back button is clicked.
29-
// NOTE(ms): It seems that the popstate event is fired on page load. Thus, it
30-
// is not necessary to bind the init() to page load independently.
31-
$(window).bind('popstate', init);
31+
// A workaround is required here because Chrome calls popstate on page load
32+
// whereas Firefox does not.
33+
$(window).bind('popstate', function (ev) {
34+
if (!window.history.ready && // set this before each history.pushState
35+
!ev.originalEvent.state)
36+
return; // workaround for popstate on load
37+
38+
init();
39+
});
3240

3341

3442
// Short-circuit 'new paste' link.
3543
$('a.new').unbind('click');
36-
$('a.new').click(function(e) {
44+
$('a.new').click(function (e) {
3745
// Browsers that support history modification can change the page state
3846
// without a refresh.
3947
if (window.history && history.pushState) {
4048
$('#_content').val(''); // clear only the content field
4149
showOnly('new');
50+
window.history.ready = true;
4251
history.pushState(null, null, '/');
4352

4453
// Stop propagation.
@@ -52,6 +61,7 @@ $('a.new').click(function(e) {
5261
// Redirects to 'url'. Employs deep magic to hide the referrer URL.
5362
function redirect(url) {
5463
if (window.history && history.replaceState) {
64+
window.history.ready = true;
5565
history.replaceState(null, null, '/');
5666
}
5767
window.location.href = url;
@@ -181,6 +191,7 @@ $('.pastearea').blur(pasteAreaBlur);
181191
function pasteSuccess(data, text_status, jq_xhr) {
182192
// Update address bar with URL of paste.
183193
if (window.history && history.pushState) {
194+
window.history.ready = true;
184195
history.pushState(null, null, data.id);
185196
loadPaste(data.id);
186197
} else {

0 commit comments

Comments
 (0)