单击链接时,window.location.hash 会继续附加名称而不是替换它
好的,我已经让这个脚本正常工作了 99%,但我似乎无法弄清楚这一点。
当用户单击链接时,它会获取 href 值并将其切碎,然后为我提供 url 的最后一段。在此示例中,我会说它的 home,
因此我的脚本会获取 home 并将其添加到地址栏中的 url。 所以它在完成它的魔力后看起来就像http://www.site.com/user/s2xi/home
。
但最初我遇到了一个问题,由于链接工作方式的本质,我想它会继续附加我的网址,如下所示 http://www.site.com/user/s2xi#/home
我的服务器不存在
所以我能够摆脱#并且它让一切恢复正常...哦等等,不,并不是所有的东西在第一次尝试时总是完美的...
所以我然后意识到我的脚本是附加链接名称而不是替换它们...哦不,现在怎么办?
我的链接现在看起来像这样: http://www.site.com/user/s2xi/home/someOtherLink
它将把新的链接名称附加到旧的链接名称,而不是替换它...
我的脚本:
var newHash = "",
shref = "",
content = '#content',
$c = $("#content"),
$cw = $("#content-wrapper");
$(".menu-link, #my-account-link").live('click', function(){
shref = $(this).attr("href").split("/");
parent.location.hash = $(this).attr("href");
window.location.hash = shref[5];
console.log(window.location.hash);
return false;
});
$(window).bind('hashchange', function(){
if (location.href.indexOf("#") > -1) {
location.assign(location.href.replace(/\/?#/, "/"));
}
newHash = window.location.hash;
//newHash = window.location.hash.substring(1);
//newHash = window.location.substring(1);
//console.log(newHash);
if(newHash)
{
$cw.find(content).fadeOut(200, function() {
$cw.load(newHash + " #content-wrapper", function() {
$c.fadeIn();
});
});
}
});
$(window).trigger('hashchange');
脚本逻辑可能有什么问题?
Ok, I have this script working 99% of how it should be working, but I can't seem to figure out this little bit.
When the user clicks on a link it takes the href value and chops it up and gives me the last segment of the url. In this example I'll say its home
so my script then grabs home and adds it to the url in the address bar.
so it look like http://www.site.com/user/s2xi/home
after its done doing its magic.
But originally I had an issue where because of the nature of how links work I suppose it would keep appending my url like this http://www.site.com/user/s2xi#/home
which on my server doesn't exists
So I was able to get rid of the # and it got everything back to normal...oh wait, no not everything always works perfect on the first try...
so i then realized that my script was appending link names instead of replacing them... oh noes, now what?
my links would now look like this: http://www.site.com/user/s2xi/home/someOtherLink
it would append the new link name to old link name instead of replacing it...
my script:
var newHash = "",
shref = "",
content = '#content',
$c = $("#content"),
$cw = $("#content-wrapper");
$(".menu-link, #my-account-link").live('click', function(){
shref = $(this).attr("href").split("/");
parent.location.hash = $(this).attr("href");
window.location.hash = shref[5];
console.log(window.location.hash);
return false;
});
$(window).bind('hashchange', function(){
if (location.href.indexOf("#") > -1) {
location.assign(location.href.replace(/\/?#/, "/"));
}
newHash = window.location.hash;
//newHash = window.location.hash.substring(1);
//newHash = window.location.substring(1);
//console.log(newHash);
if(newHash)
{
$cw.find(content).fadeOut(200, function() {
$cw.load(newHash + " #content-wrapper", function() {
$c.fadeIn();
});
});
}
});
$(window).trigger('hashchange');
what could be wrong with the script logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果没有
/.../g
,您只需替换第一个匹配项,这可能就是您想要的。您的逻辑似乎是:
换句话说,假设它以:
http://www.site.com/user/s2xi
例如,您的 href 是 http://www.site.com/user/s2xi/home< /a>.然后点击,你会得到 shref[5] = "home"。然后将其添加到当前网址的哈希中,这使得它:
http ://www.site.com/user/s2xi#home
然后将该哈希值转换回链接:
http://www.site.com/user/s2xi/home
一切都很好。第二次单击链接时,例如 http://www.site.com/user/ s2xi/foo。单击后,shref[5] = "foo"。
然后将其添加到当前 url 的哈希值中:
http://www.site.com /user/s2xi/home#foo
然后将该哈希值转换回链接:
http://www.site.com/user/s2xi/home/foo
瞧。您附加了链接而不是替换它。您应该替换 url 中的段。
First, without
/.../g
, you're just replacing the first match, which may be what you want.You logic seesm to be:
In other words, say it starts with:
http://www.site.com/user/s2xi
You href, say, is http://www.site.com/user/s2xi/home. Then on your click, you get shref[5] = "home". And you add it to hash of the current url, which makes it:
http://www.site.com/user/s2xi#home
And then you convert that hash back into a link:
http://www.site.com/user/s2xi/home
All is fine. The second time you click on a link, say, http://www.site.com/user/s2xi/foo. On your click, shref[5] = "foo".
You then add it to your hash of the current url:
http://www.site.com/user/s2xi/home#foo
Then you convert that hash back into a link:
http://www.site.com/user/s2xi/home/foo
Voila. You appened the link instead of replacing it. You should be replacing the segment in the url.