17

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

        });

       }

    });
1
  • Please provide more information. What is your jQuery like? Commented Mar 5, 2010 at 10:11

4 Answers 4

29

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 #

Sign up to request clarification or add additional context in comments.

1 Comment

I need to remove it totaly as it is causing some AJAX to fail.
14

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

3 Comments

There is no need for you to be using jQuery to execute this statement.
hmm.. this is leaving me with mypage.htm# ... am i missing something? i'd like to see mypage.htm
With this solution, some browsers leave the hash on the end, and some don't.
8

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

this.hash.slice(1);

This will give #123 as 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.

Comments

2

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

window.location.hash="!";

1 Comment

That's great. Cannot believe I asked this question almost ten years ago lol

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.