Created
July 24, 2013 09:55
-
-
Save ticklemynausea/6069314 to your computer and use it in GitHub Desktop.
Navigation buttons and ajax navigation via history API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Browser State | |
| */ | |
| /* | |
| * Pushes into the state stack. Elem is the element used to change the state, usually <a> | |
| */ | |
| function pushState(elem) { | |
| var history = window.history; | |
| if (history === undefined) { | |
| return false; | |
| } | |
| var href = $(elem).attr("href"); | |
| var rel = $(elem).attr("rel"); | |
| var obj = {href:href, rel:rel}; | |
| try { | |
| history.pushState(obj, null, ""); | |
| //console.log("PSH", obj); | |
| } catch (e) {} | |
| } | |
| $(function() { | |
| $(window).bind("popstate", function(e) { | |
| var history = window.history; | |
| if (history === undefined) { | |
| return false; | |
| } | |
| var state = e.originalEvent.state; | |
| //console.log("statechange", state); | |
| if ((state == null) || (state === undefined)) { | |
| //console.log("NO MORE STATES"); | |
| return false; | |
| } | |
| //console.log("POP", state); | |
| // Alterar location | |
| var a = $('<div id="fake"><a class="ajax" href="'+state.href+'" rel="'+state.rel+'">TESTE</a></div>'); | |
| $(state.rel).append(a); | |
| bind_ajax_links("div#fake"); | |
| $("div#fake a").click(); | |
| // Links navegação top | |
| var url = state.href; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment