jQuery从URL中删除哈希值 [英] jQuery removing hash value from URL

查看:58
本文介绍了jQuery从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

当启用Javascript时,我不想在最后使用哈希值,所以如何删除它?

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.

谢谢。

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

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:

        // 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>'));

        });

       }

    });


推荐答案

原始



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

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.

查看其他stackoverflow问题,

looking on other stackoverflow questions,

parent.location.hash = ''

应该这样做,但可能重新加载页面(你必须测试它)

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

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

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

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



编辑2,代码基于发布的代码



或者,如果你只需要用 url 调用AJAX而不用哈希,你可以在字符串中删除它,调用jQuery,不是吗?

edit 2 with code based on posted code

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

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

我们基本上先得到一个 href code>#

we basically get a's href before first #

这篇关于jQuery从URL中删除哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆