Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No, it's not possible, not directly anyway. <a href="https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState%28%29.C2.A0method">According to MDC</a> the "state object," i.e. the first argument to <code>pushState</code>, "can be anything that can be serialized." Unfortunately, you can't serialize a function. The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-history-pushstate">WHATWG spec</a> says basically the same thing but in many more words, the gist of which is that functions are explicitly disallowed in the state object.</p> <p>The solution would be to store either a string you can <code>eval</code> or the name of the function in the state object, e.g.:</p> <pre><code>$(window).bind("popstate", function(event) { var state = event.originalEvent.state; if ( !state ) { return; } window[ state.callback ]( state.argument ); // &lt;-- look here } function beforeLoad() { var resourceId = "xyz", func ; if ( case1 ) { func = "switchPageToMode1"; // &lt;-- string, not function } else { // case 2 func = "swithPageToMode2"; } window[ func ]( resourceId ); // &lt;-- same here window.history.pushState( { callback : func, argument : resourceId }, "newTitle", "newURL" ); } </code></pre> <p>Of course that's assuming <code>switchPageToMode1</code> and <code>-2</code> are in the global context (i.e. <code>window</code>), which isn't the best practice. If not they'll have to be accessible somehow from the global context, e.g. <code>[window.]MyAppGlobal.switchPageToMode1</code>, in which case you would call <code>MyAppGlobal[ func ]( argument )</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload