jQuery removing hash value from URL

戏子无情 提交于 2019-11-30 03:21:10

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 #

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

Spiderkeg

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.

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

window.location.hash="!";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!