简体   繁体   English

jQuery从URL中删除哈希值

[英]jQuery removing hash value from URL

I have a hard coded URL like so: 我有一个像这样的硬编码网址:

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. 这是我正在使用的AJAX jQuery。 So i am pasisng the hard coded URL to the same page on the server and retrieving a table from it: 所以我将硬编码的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>'));

        });

       }

    });

original 原版的

It depends on what the hash value does. 这取决于哈希值的作用。 If it just moves the document down to #a1 , you just need to set scrollTop to 0 after document has been loaded probably. 如果它只是将文档向下移动到#a1 ,您只需要在加载文档后将scrollTop设置为0。

edit 编辑

looking on other stackoverflow questions, 查看其他stackoverflow问题,

parent.location.hash = ''

should do it, but maybe reloads the page (you have to test it) 应该这样做,但可能重新加载页面(你必须测试它)

Other than that, I advice you to handle it during/before your AJAX calls - ie 除此之外,我建议你在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 url without hash, 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 href before first # 我们基本上先得到一个href #

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

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)); 编辑:我应该注意,如果你要从这些数据中计算数字,最好使用parseInt(this.hash.slice(1)); or else you'll get funky results. 否则你会得到时髦的结果。

This works for me. 这适合我。 I have added a ! 我添加了一个! to prevent the page from scrolling up. 防止页面向上滚动

window.location.hash="!";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM