Javascript jQuery 从 URL 中删除哈希值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2385897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:03:10  来源:igfitidea点击:

jQuery removing hash value from URL

javascriptjqueryfragment-identifier

提问by RyanP13

I have a hard coded URL like so:

我有一个硬编码的 URL,如下所示:

https://bupacouk.bwa.local.internal.bupa.co.uk/cash-plan-quote/quoteAction.do?getBenefitLevelDetails=getBenefitLevelDetails&productPolicyId=7841#a1

When Javascript is enabled i don't want the hash value on the end so how do i remove it?

启用 Javascript 后,我​​不想要哈希值,所以如何删除它?

When Javascript is disabled it needs to be present.

当 Javascript 被禁用时,它需要存在。

Thanks.

谢谢。

EDIT

编辑

Here is the AJAX jQuery that i am using. So i am pasisng the hard coded URL to the same page on the server and retrieving a table from it:

这是我正在使用的 AJAX jQuery。所以我将硬编码的 URL 传递到服务器上的同一页面并从中检索表:

        // Find href of current tab
    var $tabValue = $(this).attr('href');

    // AJAX new table in
    $.ajax({
        type: "GET",
        cache: false,
        url: $(this).attr('href'),
        success: function(data){

        // Find benefit wrap
        $(data).find('.benefitWrap').each(function(){
            // get the contents
            var $benefitWrap = $(this).html();
            // replace contents on page
            $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));

        });

       }

    });

回答by Adam Kiss

original

原来的

It depends on what the hash value does. If it just moves the document down to #a1, you just need to set scrollTopto 0 after document has been loaded probably.

这取决于哈希值的作用。如果它只是将文档向下移动到#a1,则可能只需要scrollTop在文档加载后设置为 0。

edit

编辑

looking on other stackoverflow questions,

查看其他stackoverflow问题,

parent.location.hash = ''

should do it, but maybereloads the page (you have to test it)

应该这样做,但可能会重新加载页面(您必须对其进行测试)

Other than that, I advice you to handle it during/before your AJAX calls - i.e.

除此之外,我建议您在 AJAX 调用期间/之前处理它 - 即

if (hash != 'a1'){ doAjax(); } //pseudocode obviously.

edit 2 with code based on posted code

使用基于发布代码的代码编辑 2

Or, if you just need to call AJAX with urlwithouthash, you can delete it in string, that calls the jQuery, no?

或者,如果您只需要在url没有哈希的情况下调用 AJAX ,您可以在字符串中删除它,即调用 jQuery,不是吗?

var $tabValue = $(this).attr('href');
var $withoutHash = $tabValue.substr(0,$tabValue.indexOf('#'));

we basically get a's hrefbefore first #

我们基本上得到的href前第一#

回答by Steve Chab

A simple window.location.hash=""will do it.

一个简单的window.location.hash=""就可以了。

回答by Spiderkeg

This might be helpful to someone asking the same question, how to pull the data following a # in a href.

这可能对提出相同问题的人有所帮助,即如何在 href 中的 # 之后提取数据。

this.hash.slice(1);

This will give #123 as 123.

这将使 #123 为 123。



Edit: I should probably note, if you're going to be calculating numbers from this data, best to use parseInt(this.hash.slice(1));or else you'll get funky results.

编辑:我可能应该注意,如果您要从这些数据中计算数字,最好使用parseInt(this.hash.slice(1));,否则您会得到时髦的结果。

回答by Alvin Konda

This works for me. I have added a !to prevent the page from scrolling up.

这对我有用。我添加了一个!以防止页面向上滚动。

window.location.hash="!";