JQuery removing hash value from url

I have a hardcoded url:

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

When Javascript is turned on, I don’t want the hash value at the end, so how to remove it?

When Javascript is disabled, it must be present.

Thank.

EDIT

Here is the AJAX jQuery I'm using. Therefore, I find the URL of the hard-coded URL on the same page on the server and extract the 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>')); }); } }); ($ ( '<div class = "benefitWrap">' + $ benefitWrap + '</ div>')).;  // 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>')); }); } }); 
+17
javascript jquery fragment-identifier
Mar 05 '10 at 10:10
source share
4 answers

original

It depends on what the hash value does. If it just moves the document to #a1 , you just need to set scrollTop to 0 after loading the document.

change

looking at other stackoverflow questions,

 parent.location.hash = '' 

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

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

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

change 2 with code based on published code

Or, if you just need to invoke AJAX using a url without a hash, can you delete it in a line that calls jQuery, no?

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

basically we get a href before the first #

+27
Mar 05 '10 at 10:12
source share

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

+14
Dec 13 '11 at 14:45
source share

This might be useful for someone asking the same question how to pull data following # in href.

 this.hash.slice(1); 

This will give No. 123 as 123.




Edit: I should notice that if you are going to calculate numbers from this data, it is best to use parseInt(this.hash.slice(1)); otherwise you will get funky results.

+8
Mar 01 '12 at 19:22
source share

This works for me. I have added ! so that the page does not scroll up.

 window.location.hash="!"; 
0
Jul 11 '19 at 16:19
source share



All Articles