Can someone help me with a regular expression that would help me do this.
I have a url: http://www.site.com/foo/bar
when a user clicks on a link it will append it to the url making it look like this
http://www.site.com/foo/bar/link1
now for the tricky part, when a second link is clicked it keeps appending to the url like this
http://www.site.com/foo/bar/link1/link2
i would like for link2 to replace link1 making it http://www.site.com/foo/bar/link2
I want to note that the only reason links are appending like this are because I am using this expression to remove the '#' from the link and replacing it with '/'
location.assign(location.href.replace(/\/?#/g, "/"))
without the # the browser will not know to replace the hash marks but its causing me issues with my current setup.
Otherwise the link will look like http://www.site.com/foo/bar#link1 witch doesn’t exist with my setup
I’m using htaccess to replace page.php?type=foo&user=bar&page=link1 http://www.site.com/foo/bar/link1
so ya, my question is how to remove /link1 with /link2 BUT append link2 IF /link1 isn’t present
I want to keep the code as dynamic as possible since the link names could change.
EDIT:
My code
var newHash = "",
shref = "",
content = '#content',
$c = $("#content"),
$cw = $("#content-wrapper");
$(".menu-link, #my-account-link").live('click', function(){
shref = $(this).attr("href").split("/");
//window.location.hash = shref[5];
//location.assign(location.href.replace(/\/?#/g, "/"))
window.location.hash = $(this).attr("href");
//console.log(window.location.hash);
return false;
});
$(window).bind('hashchange', function(){
/*if (location.href.indexOf("#") > -1) {
location.assign(location.href.replace(/\/?#/g, "/"));
}*/
//TODO:: Find # replace with / then find first (or maybe 3rd/) and replace everything until the next / with ""
newHash = window.location.hash.substring(1);
console.log(newHash);
if(newHash)
{
$cw.find(content).fadeOut(200, function() {
$cw.load(newHash + " #content-wrapper", function() {
$c.fadeIn();
});
});
}
});
$(window).trigger('hashchange');
1 Answer