Jignesh Panchal
Jignesh Panchal

Reputation: 1376

How to remove last text after / from location.href using jQuery?

I am trying to remove #step_4#step_4#step_4 from my local project link

https://localhost/test-project/#step_4#step_4#step_4 using window.location.href.

I have done below code for that

var url_id = window.location.href;
var url_id_value = url_id.split('#')[1];
console.log(url_id.split('#')[1]);

But I just got step_4 in console.

But I want to store a value https://localhost/test-project/ in variable using window.location.href or is there any other way to store that value in variable?

Upvotes: 0

Views: 198

Answers (1)

ScareCrow
ScareCrow

Reputation: 525

Replace the line of code var url_id_value = url_id.split('#')[1]; with the following:

var url_id_value = url_id.substr(0, url_id.lastIndexOf("/") + 1);

Upvotes: 1

Related Questions