javascript - How to name a jQuery function to make a browser's back button work? -
i'm trying write function ajaxyfy web site, works perfect, except function button.
that's have now:
function loading() { if (typeof history.pushstate !== "undefined") { var historycount = 0; $('.menu-item, .logo').on('click','a',function(e){ e.preventdefault(); $(this).data('clicked', true); if ($(this).is('.logo a')) { var homepage = $(this).attr('href'); function1(homepage); history.pushstate(null, null, homepage); } else if ($(this).is('.projects a')) { var projects = $(this).attr('href'); function2(projects); history.pushstate(null, null, projects); } else { var pages = $(this).attr('href'); function3(pages); history.pushstate(null, null, pages); } }); window.onpopstate = function(){ if(historycount) { goto(document.location); } historycount = historycount+1; }; } } function function1(homepage) { $.ajax({ url: homepage, success: function(data) { $('.container_right_wrapper').hide('slide', {direction: 'left'}, 300, function(){ $(this).html(data).show('slide', {direction: 'right'}, 300); console.log('home'); }); } }); } function function2(projects) { $.ajax({ url: projects, success: function(data) { $('.container_right_wrapper').hide('slide', {direction: 'left'}, 300, function(){ $(this).html(data).show('slide', {direction: 'right'}, 300); console.log('projects'); }); } }); } function function3(pages) { $.ajax({ url: pages, success: function(data) { $('.container_right_wrapper').hide('slide', {direction: 'left'}, 300, function(){ $(this).html(data).show('slide', {direction: 'right'}, 300); console.log('page'); }); } }); }
and there problem with
window.onpopstate = function(){ if(historycount) { goto(document.location); } historycount = historycount+1; };
if leave goto doesn't work @ all, if put name of other function, i.e. function1, works function 1. how make work 3 functions regardless of function has been triggered?
edit
what i'm concrned function:
window.onpopstate = function(){ if(historycount) { goto(document.location); } historycount = historycount+1; };
it's name is, can seen, goto (because related function named goto(href)). in if conditional tags call functions: function1, function2, function3, replaced earlier existent function goto(href). when divided function 3 idividual functions, above window.popstate... function stopped work. need have 3 different function because need 3 different thing happen when clicking on different menu buttons. can replace goto function1 or function2 ...etc, button works oneof 3 functions. , want work of them.
well, managed myself. digged bit deeper html5 push.state , and made following changes:
i changed
history.pushstate(null, null, homepage);
to:
history.pushstate('function1', null, homepage);
i did analogous changes 2 other push.state functions. replaced
window.onpopstate = function(){ if(historycount) { goto(document.location); } historycount = historycount+1; };
with:
window.onpopstate = function(){ if(historycount) { var jsonattr = (json.stringify(event.state).replace(/\"/g, "")); window[jsonattr](document.location); } historycount = historycount+1; };
what pulls name of every of 3 functions push.state, , that, every browser's button hit, desired function being executed.
Comments
Post a Comment