jquery - js get the window.location.hash and split it into an array and replace certain array element -
hey extremely smart people!
i have been boggled , have tried week , can't find logical solution.
basically whenever user clicks on either 2 different sets of links want prepared url appended.
so far in code happens
$(selecter a.1).live('click', function(){ window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/'); }
this make url like: http://www.site.com/!#/username/page1/
then call function detect hash change in url , return url should be:
$(window).bind('hashchange', function(){ hash = window.location.hash; //if( hash.indexof( "/files/" ) == 6 ) //newhash = window.location.hash.substring(3).replace(usr + '/files', '').replace('/', '.php'); //else newhash = window.location.hash.substring(3).replace(usr + '/', '').replace('/', '.php'); //newhash = window.location.hash.replace(dump, orighash, 'g'); console.log(newhash); if (newhash) { $wrap.find("#main-content").fadeout(200, function() { $wrap.load(newhash + " #main-content", function() { $content.fadein(function() { $wrap.animate({ height: baseheight + $content.height() + "px" }, 500); }); }); }); } });
no problems there, problem when user click on second set of links want append little differently url.
$(selecter a.2).live('click', function(){ window.location.hash = '!/' + usr + '/' + $(this).attr('href').replace('.php', '/'); }
will this: http://www.site.com/!#/username/files/sub-page/
the href of links href="files/sub-page.php"
the problem have binding window hashchange attempt prepare url original 'state' applicable first set of links.
i had idea of why not replace orginal href value window.location.hash realized well... if user reloads page?? duh! original hash lost! , url dependant in detection of hash change can load proper php page.
ok, next thought maybe can somehow slice out except last word in in url , replace('/', '.php') extension tacked on...
ok, other issue in url pointing files/sub-page.php located in files/ directory.
i had in 1 directory , wanted manually append files/ url running in problems same logic, thought move them folder called files/ , figure out how trick js leaving dir path in there...
ok, thats story...
can please help?
you're in luck: https://github.com/allmarkedup/jquery-url-parser
parses url , provides helper methods on how access each bit of url!
Comments
Post a Comment