0

This is different from existing queries here.

I am facing removing #url when destination id is in different page.

Here's what I've done : There are 2 HTML pages : index.html & about.html. I have shown various Menu information using <div id="one"></div> in index.html page rather than separate page for every menu item, to remove #url when clicked I have tried the below lines separately.

history.pushState('', document.title, window.location.pathname);

var target = $(this.target);

However, #url is not showing only in index.html page. But, when clicked on menu items in about.html page the #url is still visible (because the <div id=""></div> are in index.html). Can you help me to remove #url even the destination id is in different page.

8

1 Answer 1

1

If I understand correctly you have two pages with identical navigation that include anchors with hash values, like: #target. But you don't have the elements that the anchors refer to at both pages, so the hash has no use on the page without the, for example: <div id="target">.

If that is the case then the snippet below could help. It reads the hash value from the location object and then tries to select the element with that hash. If no element is found on the page, the URL will be updated. Place this script on every page and run it instantly.

var hash = location.hash;
var $targets = $(hash);
if (!$target.length) {
  history.pushState('', document.title, location.pathname);
}
Sign up to request clarification or add additional context in comments.

Comments

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.