admin管理员组文章数量:1425717
I have a scroll plugin that uses my div ID to scroll to a specific anchor,
It's creating urls like this: |700
I want to find a way using js or any other suggested method to hide the hash in the url
I want to transform this: |700
into this: /
Any ideas?
I have a scroll plugin that uses my div ID to scroll to a specific anchor,
It's creating urls like this: http://example./#examplediv|700
I want to find a way using js or any other suggested method to hide the hash in the url
I want to transform this: http://example./#examplediv|700
into this: http://example./
Any ideas?
Share Improve this question edited Mar 12, 2015 at 3:48 Barmar 784k57 gold badges548 silver badges659 bronze badges asked Mar 12, 2015 at 3:45 BANGOBANGO 572 silver badges9 bronze badges3 Answers
Reset to default 2You can remove it, save it and scroll to the anchor, which has the same effect.
// Add smooth scrolling to links
$(".anchor-link").on('click', function(event) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800);
});
But works only for anchors at the same page. What if the anchor is at a different page?
You can either modify the scroll plugin you are using or add it yourself on the side but you will want to do something like this:
Assumption: All DIVs that you are concerned with regarding this scrolling will need to have the anchor-scrolls
class.
HTML
<a href="#anchor-hash" class="anchor-scrolls">foo</a>
JS
//using jQuery
(function($){
$('.anchor-scrolls').on('click', function(evt){
evt.preventDefault(); //prevents hash from being append to the url
});
)(window.jQuery);
The hash is in the location.hash
property. Just set it to the empty string. If you need to use it in the rest of your code, you can save it in another variable first.
var saved_hash = location.hash;
location.hash = '';
本文标签: javascriptHow to hidehash in a url using jsStack Overflow
版权声明:本文标题:javascript - How to hide # hash in a url using js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745454269a2659028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论