1

I am using History.js to make my ajax pages bookmarkable and to have the expected back/forward browser button experience. In Safari all works great but in Firefox 6 if I leave my site and then go back using hte back button I get the javascript instead of the page:

  1. go to mysite.com/user/1/post_board
  2. click on post filter, address changes to mysite.com/user/1/post_board?filter=one
  3. enter google.com in address bar
  4. push the back button
  5. I see the javascript loaded at step 2

What is going? What I am doing wrong?

Here is the code:

//  Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(e)
    {
        var state = History.getState();
        eval(state.data.function_rest + "("+JSON.stringify(state)+");");        
        History.log(state);
    });


$('.message_filter').bind('click', function() 
    {
        History.pushState({"function_rest":"restore_post_board_widget"}, document.title, this.href);
        return false
    });

function restore_post_board_widget(state)
{   
    $.getScript(state.url);
}

Thanks for any help.

Shef
  • 44,808
  • 15
  • 79
  • 90
Matteo Melani
  • 2,706
  • 2
  • 24
  • 30
  • [Not another use of eval](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval#Don't_use_eval!). :/ – Shef Sep 11 '11 at 07:34
  • Does the url `mysite.com/user/1/post_board?filter=one` always return HTML? Or does it sometimes return JavaScript? – Boris Zbarsky Sep 11 '11 at 13:45
  • @Shef: I am open to suggestion on how to handle the history management. Using eval allows quite a bit of flexibility but again I am a total newbie to Javascript: http://stackoverflow.com/questions/7340906/whats-the-recommended-way-to-rebuild-a-page-from-the-browser-history-history-j – Matteo Melani Sep 11 '11 at 16:21

1 Answers1

2

just add:

window.onunload = function(){};

this is where I got the answer:

After travelling back in Firefox history, JavaScript won't run

Community
  • 1
  • 1
Matteo Melani
  • 2,706
  • 2
  • 24
  • 30
  • That's the *worst* solution to your problem, it degrades browser performance. See http://stackoverflow.com/questions/7194095/differences-between-chrome-and-firefox-reloading-a-page-running-a-javascript-gam/7200707#7200707 for the other options. – Wladimir Palant Sep 12 '11 at 07:02
  • Thanks for pointing that out but even after reading the question you pointed to I do not see any other alternative. Can you point me in the right direction? Thanks. – Matteo Melani Sep 13 '11 at 15:49