Skip to content
the poor coder

remove hash js

Poor Man
Published
Reading time
2 min read
On this page

How to Remove Hash Js from Your Website#

If you're a web developer, you may have come across hash js in your code. A hash js is a method that adds a "#" to the URL of a page, which is often used by single-page applications (SPAs) to keep track of routing and state. However, if you're not using an SPA, it can be unnecessary and even cause issues with SEO.

Method 1: Removing Hash Js with JavaScript#

If you're using hash js in your code and want to remove it, one way to do so is by using JavaScript. You can do this by placing the following code in a script tag on your HTML page:

Text
// Remove Hash Js from URL
if (window.location.hash && window.location.hash === '#_=_') {
  window.location.hash = '';
}

This code checks if the URL has a hash and if it's equal to "#_=_", which is often added by Facebook authentication. If it matches, the code removes the hash from the URL.

Method 2: Removing Hash Js with Server-side Code#

If you're using a server-side framework like PHP, you can remove hash js by adding the following code to your .htaccess file:

Text
RewriteCond %{REQUEST_URI} ^/.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /#$1 [NE,R=301,L,QSA]

This code redirects any URLs with a hash to the same URL without the hash. It also preserves any query strings that may be present in the URL.

Method 3: Using HTML5 History API#

If you're using an SPA and want to remove hash js, you can use the HTML5 History API. This allows you to change the URL of a page without causing a full page refresh.

You can use the following code to replace the current URL without the hash:

Text
// Replace Hash Js with HTML5 History API
window.history.replaceState({}, document.title, window.location.pathname);

This code replaces the current state of the History object with an empty object and the current URL without the hash. This effectively removes the hash from the URL without causing a page refresh.

Conclusion#

There are multiple ways to remove hash js from your website, depending on your use case and coding approach. Whether you choose to use JavaScript, server-side code, or the HTML5 History API, removing hash js can improve the SEO and user experience of your website.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC