Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

When Javascript is disabled it needs to be present.

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:

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

        });

       }

    });
like image 431
RyanP13 Avatar asked Mar 05 '10 10:03

RyanP13


People also ask

How to remove hash from URL?

To remove the hash URL, you can use the replaceState method on the history API to remove the hash location. Example: HTML.

What is the hash in URL?

In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. When you use a URL with a # , it doesn't always go to the correct part of the page or website.


2 Answers

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.

edit

looking on other stackoverflow questions,

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 - i.e.

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

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('#'));

we basically get a's href before first #

like image 157
Adam Kiss Avatar answered Oct 04 '22 17:10

Adam Kiss


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

like image 38
Steve Chab Avatar answered Oct 04 '22 15:10

Steve Chab