1

I have a scroll plugin that uses my div ID to scroll to a specific anchor,

It's creating urls like this: http://example.com/#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.com/#examplediv|700 into this: http://example.com/

Any ideas?

3 Answers 3

2

You 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?

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

Comments

1

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

Comments

1

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

4 Comments

That won't "hide it", though, that will remove it (which could be fine if your application doesn't need it anymore).
My application needs it, but i just dont want it to show in the browser url
You can save it in another variable before changing the location.
@Barmar Could you provide me with an example, Im still a newbie in js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.