JQuery.address如何从url中删除#(javascript window.location删除#)

发布于 2024-10-04 00:04:15 字数 928 浏览 0 评论 0原文

当我的 event.value == 到 / 时,我需要从 url 中删除 #。 我有一个带有 jquery.address 的 lightbox,它存储对打开图像的引用,当我关闭它时,我需要删除 # 标记,因为这会导致窗口滚动到顶部。

我成功删除了 # 标记: window.location.href.slice(0, -1);但正如您在上面的代码中看到的,这会导致在加载页面时进行 url 重写,而不仅仅是在我的活动之后。

仅当我完成时,我如何才能链接此 JavaScript,就像当我关闭灯箱时此函数被称为 olny 一样。

我在这里附上我的代码和注释。 谢谢大家

$.address.change(function(event) {
    curLink = event.value;
    if(curLink != '/') {

    // here my stuff, jquery.address generates url with reference 

    // ex: mysite.com/cat/subcat/page.html#one

    } else {  

        $('#element').animate({opacity:"0"},{duration:100, easing:"quartEaseOut", complete: function () {

        // here I need to remove the hash only after the complete 

        // ex: mysite.com/cat/subcat/page.html#  >  mysite.com/cat/subcat/page.html

        window.location.href.slice(0, -1);           
        $(this).hide(); 
    }});   
  }   
});

I need to remove the # from url when my event.value is == to /.
I've got a lighbox with jquery.address that stores references to open images, when i close it i need to remove the # mark becouse this cause window scroll top.

I succed remove the # mark with this: window.location.href.slice(0, -1); but as you can see in the code above this cause the url rewrite when page is loaded and not only after my event.

How can i chain this javascript only when my complete happens, in the way this function is called olny when i close the lightbox.

I attach here my code with comments.
thank you all

$.address.change(function(event) {
    curLink = event.value;
    if(curLink != '/') {

    // here my stuff, jquery.address generates url with reference 

    // ex: mysite.com/cat/subcat/page.html#one

    } else {  

        $('#element').animate({opacity:"0"},{duration:100, easing:"quartEaseOut", complete: function () {

        // here I need to remove the hash only after the complete 

        // ex: mysite.com/cat/subcat/page.html#  >  mysite.com/cat/subcat/page.html

        window.location.href.slice(0, -1);           
        $(this).hide(); 
    }});   
  }   
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫﹏色ふ单纯 2024-10-11 00:04:16

你也可以使用这个(它完全删除哈希值并清理 URL),

history.pushState("", document.title, window.location.pathname);

它会更改

http://www.google.com/#top

http://www.google.com/

You could also use this (which totally removes the hash and cleans the URL right back up)

history.pushState("", document.title, window.location.pathname);

Which changes

http://www.google.com/#top

to

http://www.google.com/
想挽留 2024-10-11 00:04:16

使用 window.location.hash = '' 应该彻底删除它,否则有一些很好的插件可以打开很多用于处理 URL 的功能(例如 http://ajaxcssblog.com/jquery/url-read-request-variables)。

Using window.location.hash = '' should remove it outright, otherwise there are some good plugins that open up a lot of functions for working with URLs (e.g. http://ajaxcssblog.com/jquery/url-read-request-variables).

柠檬 2024-10-11 00:04:16

如果您有锚点,请尝试此操作

newHash = window.location.hash.substring(1);

console.log(newHash);

,那么它通常会附加在 URL 的开头,因此删除第一个字符后,您就不应该再将其视为问题了。

或者如果你甚至可以这样做,我认为这就是你可能需要的

newHash = window.location.hash.replace('#', '');

console.log(newHash);

try this

newHash = window.location.hash.substring(1);

console.log(newHash);

if you have an anchor then its usually appended at the beginning of the URL so but removing the first character then, you shouldn't have it as an issue anymore.

or if you can even do it this way which i think is what you might need

newHash = window.location.hash.replace('#', '');

console.log(newHash);
荆棘i 2024-10-11 00:04:16

我发现在哈希之后更改值最适合与您类似的场景。它必须是您的页面上尚不存在的哈希值。

function removeDeepLink(event) {
    window.location.hash = 'foo';
};

I've found that changing the value after the hash worked best for a scenario similar to yours. It would need to be a hash that doesn't already exist on your page.

function removeDeepLink(event) {
    window.location.hash = 'foo';
};
听你说爱我 2024-10-11 00:04:16

window.location.hash = '' 在我的情况下不起作用。

window.location.href.slice(0, -1); 有效!我无法解决的问题是如何仅在完成 ​​jQuery 事件之后才链接此 JavaScript 指令(现在它是在页面加载时触发的)。

我认为现在在 jQuery 之后调用 JavaScript 是愚蠢的,

谢谢

window.location.hash = '' don't work in my case.

window.location.href.slice(0, -1); works! the problem i can't solve is how I can chain this JavaScript instruction only after the complete jQuery event (by now it is fired on page load).

I think by now is stupid JavaScript call after jQuery

thank you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文